Jump to content

[SOLVED] When a query only brings back ONE record....


galvin

Recommended Posts

This is PHP 101, I know, but if you know your query is bringing back only ONE line, then how can you get the info in that one line?  It seems overkill to do a "while" loop if there is only one record being found.

 

The code I did below is not working anyway, so can anyone tell me how to just find that ONE record that is brough back?  In other words, I know this query is being back an array with only one entry in it and I need to get that piece of information (in this case, "orderID")....

 

 

$query = "SELECT orderID FROM orders WHERE 'sessionID' = '$sessionid'";

								$getorderID = mysql_query($query, $connection);
								while($row = mysql_fetch_array($getorderID)) {
								$orderID = $row['orderID'];
								}	

if you know only 1 result will be returned then

 

<?php
$query = "SELECT orderID FROM orders WHERE 'sessionID' = '$sessionid'";
                     
                           $getorderID = mysql_query($query, $connection);
                           $row = mysql_fetch_array($getorderID);
                           $orderID = $row['orderID'];
?>

 

Personally I prefer to use mysql_fetch_assoc() but thats just me (try using the OOP mysqli too - its much nicer)...

Well, if you only want ONE row, then you should definitely append LIMIT 1 on the end of your SQL statement.  Then you can use mysql_fetch_row to get the single row:

$query = "SELECT orderID FROM orders WHERE 'sessionID' = '$sessionid' LIMIT 1";

$getorderID = mysql_query($query, $connection);
$row = mysql_fetch_row($getorderID, MYSQL_ASSOC);
echo $row['orderID'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.