Reputation: 344
I want to echo out one field from my database so I do not want to use a while
loop.
The database table is called index
and the field that I want to echo is called title
.
What is wrong with this code as the output is just blank.
$result = mysql_query("SELECT * FROM index");
$row = mysql_fetch_array($sql);
echo $row['title'];
Upvotes: 2
Views: 5493
Reputation: 8597
You may want to add LIMIT or check your database against something
$result = mysql_query("SELECT * FROM index WHERE id='$someid' LIMIT 1");
Upvotes: 0
Reputation: 29985
The fastest solution would be mysql_result
$result = mysql_query('SELECT title FROM index LIMIT 1');
$field = mysql_result($result, 'title');
Upvotes: 1
Reputation: 19037
You're passing a wrong argument to mysql_fetch_array()
. Modify it as follows.
$result = mysql_query("SELECT * FROM index");
$row = mysql_fetch_array($result);
echo $row['title'];
You need to pass $result
and not $sql
with the mysql_fetch_array()
function.
Upvotes: 4
Reputation: 100195
Try:
$row = mysql_fetch_array($result); print_r($row); ///see what you get
Upvotes: 1