15

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?

3

12 Answers 12

35

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'];
Sign up to request clarification or add additional context in comments.

3 Comments

Outed answer. mysql_* is deprecated.
$result = mysqli_query($connection, "SELECT option_value FROM wp_10_options WHERE option_name='homepage' LIMIT 1"); $row = mysqli_fetch_assoc($result); echo $row['option_value'];
5

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

Comments

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

Comments

1

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

Comments

1

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

Comments

1

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

Comments

0

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?

Comments

0

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.

Comments

0

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

Comments

0

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

Comments

0

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);
?>

Comments

-2

make sure your ftp transfers are in binary mode.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.