PHPBuilder - A Test To See If You Write Sloppy Software Page 3



RSS Twitter
Articles Tricks And Hacks

A Test To See If You Write Sloppy Software - Page 3

by: Tim Perdue
|
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";

}

?>

« Previous Page
1
|
2
|
3
|
4
|
5
|
6
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Tim Perdue

Comment:



Comment:

(Maximum characters: 1200). You have characters left.