View Full Version : Query Question

12-26-2006, 07:57 PM
How do you redirect the PHP to do something else when it returns an empty set?

$query = mysql_query("SELECT * FROM usrinfo WHERE email = '$email'") or die("died");
while($u=mysql_fetch_array($query)) {

12-26-2006, 08:27 PM
Several ways. I'm not sure of the 'official' way.
But... here are the options.
while ($u=mysql_fetch_array($q)) {
$check = 1;
if ($check != 1) {

if (!mysql_fetch_assoc($q)) {
else while (...) {}

if (mysql_num_rows($q) == 0) {
else while (...) {}


Very simple examples, but you can apply them how you need.

Not sure which one is best.

It depends on the situation. It is always easier if you are returning a single piece of data, because you can then use an if statement instead of while. But if you are returning a bunch of data, which seems likely, I'm not sure what is best. Using mysql_num_rows() might be best because it's a quick check then isn't repeated each time through the loop like the $check variable method. But that one is easy, I suppose.

Note: the use of else above seems a bit awkward, and isn't really needed, since it wouldn't do the loop because the while condition is never true. But at least the else is a faster way for the processor to avoid running the mysql_ function.

12-27-2006, 09:11 PM
#1 worked perfect, thanks...