Display alias from query statement

Very simple.
How do I reference the variable "Picture_Numb" from the following statement:

$SqlString2 = "SELECT COUNT(SEQ) AS Picture_Numb
                              FROM bldrpix
                              WHERE SEQ = '$work'";

I need to build a string as follows:
$work = (9311 + $Picture_Numb);

Thanks
breeze351Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
If you're using MySQLi, this (untested) would be a good starting point ;-)  You may want to add error checking, but these four statements should be enough to get the data element you need.

// QUERY STRING
$SqlString2 = "SELECT COUNT(SEQ) AS Picture_Numb FROM bldrpix WHERE SEQ = '$work'";

// RUN THE QUERY
$res = $mysqli->query($sql);

// RETRIEVE THE ROW
$row = $res->fetch_object());

// EXTRACT THE DATA ELEMENT
$Picture_Numb = $row->Picture_Numb;

Open in new window

0
breeze351Author Commented:
I'm not using "MySQLi".  It's just "MySql".
0
Ray PaseurCommented:
In that case you probably want to read this article while I go back to the books and create a MySQL example!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Ray PaseurCommented:
I think this might do the trick.  As you can see the construction differences between MySQL and MySQLi are almost negligible - a few characters here and there, very similar PHP instructions.  It's an easy conversion, and it's an important conversion.  The PHP community (that includes you and me) is dead serious about doing away with the MySQL extension.  It's dangerous from a security standpoint and it leads you automatically into programming practices that make your code look like something from the 1990's.  You cannot mock and test MySQL code.  It's just a bad practice from a bygone era.  So do yourself a favor and bring your code base up to modern standards.  You'll thank me later!

// QUERY STRING
$SqlString2 = "SELECT COUNT(SEQ) AS Picture_Numb FROM bldrpix WHERE SEQ = '$work'";

// RUN THE QUERY
$res = mysql_query($sql);
if (!$res) trigger_error( mysql_error(), E_USER_ERROR );

// RETRIEVE THE ROW
$row = mysql_fetch_object($res);

// EXTRACT THE DATA ELEMENT
$Picture_Numb = $row->Picture_Numb;

Open in new window

0
Chris StanyonWebDevCommented:
Here's an option for using PDO:

// Prepare the query
$stmt = $dbh->prepare("SELECT COUNT(SEQ) AS Picture_Numb FROM bldrpix WHERE SEQ = ?");

// Execute the query
$stmt->execute(array($work));

// Fetch the Picture_Numb column
$Picture_Numb = $stmt->fetchColumn();

// Calculate the result
$work = (9311 + $Picture_Numb);

Open in new window

0
Ray PaseurCommented:
^^ Chris: +1 for that!  I think Laravel uses PDO.  Might be that WordPress is going that direction, too.
0
Chris StanyonWebDevCommented:
Thanks Ray - I chose PDO over mySQLi a while ago, but for the life of me can't remember why???
0
Ray PaseurCommented:
Chris: I think either is OK as long as you can mock the objects and write unit tests.  My instinct as I answer questions here is to choose MySQLi because I know that most of the authors of questions here are not likely to write unit tests and are more likely to copy and paste solution sets -- and MySQLi makes (or can make) that a little bit easier.
0
breeze351Author Commented:
Ray:
You guys are not answering the question.
Where is $SqlString2 in your reply?
It's not in the query statement.  How would the results be returned?
0
Chris StanyonWebDevCommented:
In Ray's code the result is returned like this:

$Picture_Numb = $row->Picture_Numb;

Open in new window


In my code, it's returned like this:

$Picture_Numb = $stmt->fetchColumn();

Open in new window


Which part are you struggling to understand? We've given you 2 different code solutions to your problem - one using mySQLi and one using PDO?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
breeze351Author Commented:
Thanks.  I actually got it myself.

<!-- Images Button //-->
            <?php
                  // Set The Value Of The Image Button
                $work = (9311 + $P_Count);
      
                  // Create The Button Value
                  $Button_Value = 'Images - &#'.$work;;
            ?>
            <td align = "center" width="14.25%">
                  <input name= "Images" type="button" id="Images" onClick="MM_goToURL('parent', 'Images.php');return document.MM_returnValue" value="<?php echo $Button_Value ?>">
            </td>
0
Chris StanyonWebDevCommented:
Thanks for the points, breeze.

I'm struggling to see how your code has any bearing to the question you asked!! The problem you seemed to be having was how to get the value out of the database. The code you've just posted shows every except that part.

Anyway, pleased you got it working (somehow!)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.