Several ways. I'm not sure of the 'official' way.
But... here are the options.
PHP Code:
<?php
//1
while ($u=mysql_fetch_array($q)) {
$check = 1;
dostuff();
}
if ($check != 1) {
doerror();
}
//2
if (!mysql_fetch_assoc($q)) {
doerror();
}
else while (...) {}
//3
if (mysql_num_rows($q) == 0) {
doerror();
}
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.
Bookmarks