Erik Price wrote:
>
> 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.
The while loop is unnecessary; however, you should use:
if( ($row = mysql_fetch_assoc( $result )) )
{
echo $row['user_id'];
echo $row['fullname'];
}
just in case no rows are returned :)
Cheers,
Rob.
--
.-----------------.
| Robert Cummings |
:-----------------`----------------------------.
| Webdeployer - Chief PHP and Java Programmer |
:----------------------------------------------:
| Mail : mailto:[EMAIL PROTECTED] |
| Phone : (613) 731-4046 x.109 |
:----------------------------------------------:
| Website : http://www.webmotion.com |
| Fax : (613) 260-9545 |
`----------------------------------------------'
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php