On Wednesday, April 10, 2002, at 07:45  AM, David Robley wrote:

>> I've been looking on the net and the php docs for a simple
>> mysql query solution, can someone please let me know
>> the best way to get the values of a query that only returns 1 record?
>> All the select query examples on the net are examples of
>> multiple records being returned, and therefor, there is repition
>> methods shown.
>> For this I don't need any repitition, I just need to know the best
>> way to get the DB values from the query into variables.
>> Cheers.
>> -Mason
> A while or similar should still work with only one record; but of course
> you could just drop the while and use the 'enclosed' code just once. If
> you have a particular sample you want to reduce, post it if you need 
> more
> help.

Yes, just last night I discovered that the while or other loop is really 
only necessary if you're retrieving possible multiple results.  You can 
do it very simply just like

$sql = "SELECT...";
$result = mysql_query($sql, $db);
$row = mysql_fetch_row($result);
echo $row[0];

And of course the above can be modified for multiple columns/array 

Someone did suggest that you use an if statement, however, in the event 
that no records are retrieved --

if (!$result = mysql_query($sql, $db)) {
   echo "No records found.";
} else {
   $row = mysql_fetch_row($result);



Erik Price
Web Developer Temp
Media Lab, H.H. Brown

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to