Link to home
Start Free TrialLog in
Avatar of breeze351
breeze351

asked on

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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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

Avatar of breeze351
breeze351

ASKER

I'm not using "MySQLi".  It's just "MySql".
In that case you probably want to read this article while I go back to the books and create a MySQL example!
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
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

Avatar of Chris Stanyon
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

^^ Chris: +1 for that!  I think Laravel uses PDO.  Might be that WordPress is going that direction, too.
Thanks Ray - I chose PDO over mySQLi a while ago, but for the life of me can't remember why???
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.
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?
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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>
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!)