Question:
PHP while loop?
jeff lemon
2008-01-21 14:45:56 UTC
Im using a while loop to echo out the results from my query, BUT there will only ever be one result, what else can I use instead of WHILE?

$query = "SELECT * FROM dvd_library WHERE dvd_id = '$dvd'";
$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result)){

//DO STUFF

}
Four answers:
the U
2008-01-21 14:52:39 UTC
If there is only one result per sql query, then the while loop will only go through once. If there are more than one results, it will go through each result. I don't know of any other ways to perform this function, but the while loop should work in every situation for an sql query.
leimeisei
2008-01-21 22:50:14 UTC
If there is only going to be one result, then you don't have to use a while loop. Just put something like this:



$query = "SELECT * FROM dvd_library WHERE dvd_id = '$dvd'";

$result = mysql_query($query) or die(mysql_error());



$row = mysql_fetch_array($result);



Then whenever you need to put out one of your results, use a line like this:

echo $row['columnname'];
bulboglia
2008-01-23 14:33:38 UTC
Use an "if" statement. That way if an empty set is returned it can be easily dealt with:



$query = "SELECT * FROM dvd_library WHERE dvd_id = '$dvd'";

$result = mysql_query($query) or die(mysql_error());



if($row = mysql_fetch_array($result)){



//DO STUFF



} else {



// DO OTHER STUFF IF NO RECORDS WERE RETURNED



}
2008-01-21 22:50:38 UTC
for



search it up on php


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...