Jump to content

getting single value from mysql query


satre

Recommended Posts

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

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.

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.

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.