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

Link to comment
Share on other sites

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