tags:

views:

29

answers:

3
    $tmp = mysql_query("SELECT commercial FROM Channels 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 Please Help

+1  A: 

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.

alexn
+1  A: 

You can use mysql_fetch_row to retrieve the value like that ...

$row = mysql_fetch_row($tmp);
$commercial = $row['commercial'];
Marius Schulz
+1  A: 

If you need just the first element, why don't you append LIMIT 1 to your query ?

a1ex07