skipping a WHILE loop in mysql_fetch_*()
| From: | Erik Price | Date: | Tue, 09 Apr 2002 20:26:42 +0000 |
| Subject: | skipping a WHILE loop in mysql_fetch_*() | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-92294@lists.php.net to get a copy of this message | ||
I have always retrieved database information using the following general format:
<?php
mysql_connect($host, $user, $password);
mysql_select_db($database);
$query = "select * from table";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
echo $row["user_id"];
echo $row["fullname"];
}
mysql_free_result($result);
?>
But sometimes I am searching for something specific, with a primary key as my WHERE clause. In other words, I will only EVER get one result. In this case, a while loop seems to be overkill, since it will only loop once.
Does it seem like a bad idea to just do:
$row = mysql_fetch_assoc($result);
echo $row['user_id'];
echo $row['fullname'];
etc? Or is there a compelling reason to use the loop? the only reason I ask is because I've never seen search query code that -isn't- in a loop of some sort. But it works fine the way I do it, immediately above.
Thanks,
Erik
----
Erik Price
Web Developer Temp
Media Lab, H.H. Brown
pricee@hhbrown.com