Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting one row from MySQL using mysql_* API

Tags:

php

mysql

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?

like image 709
1321941 Avatar asked May 15 '12 16:05

1321941


People also ask

How do I select a specific row in SQL?

To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols. You can create multiple row conditions, and use the AND, OR, or IN keywords to connect the conditions.

How do I select a specific row?

Select the row number to select the entire row. Or click on any cell in the row and then press Shift + Space. To select non-adjacent rows or columns, hold Ctrl and select the row or column numbers.

How do I select a specific field in MySQL?

If you want to select only specific columns, replace the * with the names of the columns, separated by commas. The following statement selects just the name_id, firstname and lastname fields from the master_name table.


2 Answers

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'];
like image 120
Moyed Ansari Avatar answered Oct 05 '22 23:10

Moyed Ansari


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'];
like image 30
Jsowa Avatar answered Oct 05 '22 23:10

Jsowa