Error Handling
The most common problem I see when reviewing code is a lack of error handling. What happens when no rows are
returned from a database query? Most often, I see a while loop like this:
returned from a database query? Most often, I see a while loop like this:
<?php
$res
=db_query("SELECT * FROM users");
while (
$row=db_fetch_array($res)) {
//show output
}
?>
But what if no rows were selected? You have a hole in the middle of the page.
It’s better to do this:
<?php
$res
=db_query("SELECT * FROM users");
if (
db_numrows($res)) {
while (
$row=db_fetch_array($res)) {
//show output
}
} else {
print
"No Users Found";
}
?>