[php] 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?

This question is related to php mysql

The answer is


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'];

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

   $row['option_value']

So why don't you? It should work.


$result = mysql_query("SELECT option_value FROM wp_10_options WHERE option_name='homepage'");
$row = mysql_fetch_assoc($result);
echo $row['option_value'];

What you should get as output with this code is:

Array ()

... this is exactly how you get just one row, you don't need a while loop. Are you sure you're printing the right variable?


is this is a WordPress? You shouldn't do it like you've done! To get option from DB use get_option!


use mysql_fetch_assoc to fetch the result at an associated array instead of mysql_fetch_array which returns a numeric indexed array.


Functions mysql_ are not supported any longer and have been removed in PHP 7. You must use mysqli_ instead. However it's not recommended method now. You should consider PDO with better security solutions.

$result = mysqli_query($con, "SELECT option_value FROM wp_10_options WHERE option_name='homepage' LIMIT 1");
$row = mysqli_fetch_assoc($result);
echo $row['option_value'];

It is working for me..

$show = mysql_query("SELECT data FROM wp_10_options WHERE
 option_name='homepage' limit 1"); $row = mysql_fetch_assoc($show);
 echo $row['data'];

this shoude work

    <?php

require_once('connection.php');

 //fetch table rows from mysql db
$sql = "select  id,fname,lname,sms,phone from data";

    $result = mysqli_query($conn, $sql) or die("Error in Selecting " . mysqli_error($conn));

    //create an array
    $emparray = array();

for ($i = 0; $i < 1; $i++) {
   $row =mysqli_fetch_assoc($result);

} $emparray[] = $row;
         echo $emparray ;
    mysqli_close($connection);
?>

make sure your ftp transfers are in binary mode.


Try this one if you want to pick only one option value.

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

Though mysql_fetch_array will output numbers, its used to handle a large chunk. To echo the content of the row, use

echo $row['option_value'];