How Can I access the first result?
$tmp = mysql_query("SELECT commercial FROM Channel开发者_运维问答s WHERE name='".mysql_real_escape_string($_POST['name'])."'");
while( $row = mysql_fetch_assoc($tmp))
{
echo $row['commercial'];
}
I only want to access the first element. not in a while loop
You can use mysql_fetch_row
to retrieve the value like that ...
$row = mysql_fetch_row($tmp);
$commercial = $row['commercial'];
Well, just remove your while loop then. This will get the first (actually current) row:
$tmp = mysql_query("SELECT commercial FROM Channels WHERE name='".mysql_real_escape_string($_POST['name'])."'");
$row = mysql_fetch_assoc($tmp);
echo $row['commercial'];
Another option is to use mysql_result:
$tmp = mysql_query('..');
$row = mysql_result($tmp, 0);
echo $row['commercial'];
Side note: If you only need one row, add LIMIT 1
to your query.
If you need just the first element, why don't you append LIMIT 1
to your query ?
精彩评论