• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 30
  • Last Modified:

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
0
breeze351
Asked:
breeze351
  • 5
  • 4
  • 3
1 Solution
 
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
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 StanyonCommented:
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 StanyonCommented:
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 StanyonCommented:
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
 
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 StanyonCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now