xProteuSx Posted October 17, 2011 Share Posted October 17, 2011 Right now I am using code such as this: $query = "SELECT somevalue FROM sometable WHERE id=1" $result = mysql_query($update) or die ('Error in query: ' . mysql_error()); $somevalue = ''; if (mysql_num_rows($result) == 1) { while($row = mysql_fetch_assoc($userstatsresult)) { $somevalue = $row['somevalue']; } } echo $somevalue; Is there a short-hand method to get that single value without the IF/WHILE stuff? Quote Link to comment https://forums.phpfreaks.com/topic/249246-efficient-way-of-pulling-single-value-from-a-database/ Share on other sites More sharing options...
kney Posted October 17, 2011 Share Posted October 17, 2011 #1 <?php $query = "SELECT something FROM sometable WHERE ID = 1"; $result = mysql_query ($query); $row = mysql_fetch_array($result); $record = $row[0]; ?> #2 <?php $sql = "SELECT something FROM sometable WHERE ID = 1"; $email = mysql_result(mysql_query($sql), 0, 0); ?> Quote Link to comment https://forums.phpfreaks.com/topic/249246-efficient-way-of-pulling-single-value-from-a-database/#findComment-1279879 Share on other sites More sharing options...
xProteuSx Posted October 17, 2011 Author Share Posted October 17, 2011 Very nice. In your second example, what do the two zero's mean? Quote Link to comment https://forums.phpfreaks.com/topic/249246-efficient-way-of-pulling-single-value-from-a-database/#findComment-1279885 Share on other sites More sharing options...
kney Posted October 17, 2011 Share Posted October 17, 2011 first 0 is the row number that's being retrieved second 0 is the name / offset of the field being retrieved http://php.net/manual/en/function.mysql-result.php Quote Link to comment https://forums.phpfreaks.com/topic/249246-efficient-way-of-pulling-single-value-from-a-database/#findComment-1279886 Share on other sites More sharing options...
xProteuSx Posted October 17, 2011 Author Share Posted October 17, 2011 Awesome, thank you sir. Quote Link to comment https://forums.phpfreaks.com/topic/249246-efficient-way-of-pulling-single-value-from-a-database/#findComment-1279887 Share on other sites More sharing options...
silkfire Posted October 17, 2011 Share Posted October 17, 2011 A variant of kney's second solution, it's even a one-liner =) list($email) = mysql_fetch_row(mysql_query('SELECT something FROM sometable WHERE id = 1')); Quote Link to comment https://forums.phpfreaks.com/topic/249246-efficient-way-of-pulling-single-value-from-a-database/#findComment-1279891 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.