When using PHP/MySQL

froger

New Member
Messages
49
Reaction score
0
Points
0
Lets say we have a test database. In the database we have a table named users. The table has 1 column, name.

Instead of using something like

<?php

$sql = mysql_query("SELECT name FROM users WHERE name='abc'");

while ($row = mysql_fetch_array($sql))
{
echo $row['name'];
}
?>

would it be faster to just use...

<?php
$sql = mysql_fetch_array(mysql_query("SELECT name FROM users WHERE name='abc'"));

echo $sql['name'];
?>

To fetch a single persons name? I always figured you would use the while statement if you wanted to echo the entire table...

<?php
$sql = mysql_query("SELECT name FROM users");

while ($row = mysql_fetch_array($sql))
{
echo $row['name'] . "<br/>";
}
?>
 

farscapeone

Community Advocate
Community Support
Messages
1,165
Reaction score
27
Points
48
If you have one row in the result you definitely don't need the while loop.
 
Top