#native_company# #native_desc#
#native_cta#

A Test To See If You Write Sloppy Software Page 3

By Tim Perdue
on March 10, 2003

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:

<?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";

}

?>