satre Posted April 4, 2010 Share Posted April 4, 2010 OK, I feel so dumb. I can't seem to find the answer on the forum or in the php manual. Is there a simple command that can replace fetching an array, can you just fetch the value somehow? $sql = mysql_query("SELECT value FROM settings WHERE name LIKE 'siteurl'"); $sqlfetch = mysql_fetch_array($sql); $rootlocation = $sqlfetch[0]; Thanks! Satre Link to comment https://forums.phpfreaks.com/topic/197554-getting-single-value-from-mysql-query/ Share on other sites More sharing options...
ignace Posted April 4, 2010 Share Posted April 4, 2010 You mean: list($value) = mysql_fetch_row($sql);? Link to comment https://forums.phpfreaks.com/topic/197554-getting-single-value-from-mysql-query/#findComment-1036815 Share on other sites More sharing options...
satre Posted April 4, 2010 Author Share Posted April 4, 2010 actually, I mean, the value in the table is only a single value, not an array. Isn't there a way just to fetch the single value returned by the query? I used the above workaround because I couldn't find the command to fetch a single value and assign it to a variable. Link to comment https://forums.phpfreaks.com/topic/197554-getting-single-value-from-mysql-query/#findComment-1036819 Share on other sites More sharing options...
the182guy Posted April 4, 2010 Share Posted April 4, 2010 There is also this way: $result = mysql_query("SELECT value FROM settings WHERE name LIKE 'siteurl'"); if(mysql_num_rows($result) > 0) { $value = mysql_result($result, 0, 0); // get the value of the first field of the first row } Note the use of mysql_num_rows to check there has been at least one row returned, the reason for this is mysql_result() will throw E_WARNING if mysql_query returns 0 rows. Link to comment https://forums.phpfreaks.com/topic/197554-getting-single-value-from-mysql-query/#findComment-1036822 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.