Skip to content
Advertisement

Selecting one row from MySQL using mysql_* API

I have the following query:

$result = mysql_query("SELECT option_value FROM wp_10_options WHERE option_name='homepage'");
$row = mysql_fetch_array($result);
print_r ($row);

and the output I am getting is:

Resource id #2

Ultimately, I want to be able to echo out a single field like so:

$row['option_value']

Without having to use a while loop, as since I am only trying to get one field I do not see the point.

I have tried using mysql_result with no luck either.

Where am I going wrong?

Advertisement

Answer

Try with mysql_fetch_assoc .It will returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows. Furthermore, you have to add LIMIT 1 if you really expect single row.

$result = mysql_query("SELECT option_value FROM wp_10_options WHERE option_name='homepage' LIMIT 1");
$row = mysql_fetch_assoc($result);
echo $row['option_value'];
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement