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'];
								}	

Link to comment
Share on other sites

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)...

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.