galvin Posted December 27, 2008 Share Posted December 27, 2008 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 https://forums.phpfreaks.com/topic/138525-solved-when-a-query-only-brings-back-one-record/ Share on other sites More sharing options...
ToonMariner Posted December 27, 2008 Share Posted December 27, 2008 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 https://forums.phpfreaks.com/topic/138525-solved-when-a-query-only-brings-back-one-record/#findComment-724276 Share on other sites More sharing options...
shlumph Posted December 27, 2008 Share Posted December 27, 2008 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 https://forums.phpfreaks.com/topic/138525-solved-when-a-query-only-brings-back-one-record/#findComment-724277 Share on other sites More sharing options...
galvin Posted December 27, 2008 Author Share Posted December 27, 2008 Thanks so much, guys! Link to comment https://forums.phpfreaks.com/topic/138525-solved-when-a-query-only-brings-back-one-record/#findComment-724291 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.