|
Posted by R. Rajesh Jeba Anbiah on 09/02/06 19:11
lawrence k wrote:
<snip>
> Which of these two functions is better, the one with error checking or
> the one without?
<snip>
> function getWeblogEntries() {
> $query = "SELECT * FROM weblogs";
> $result = mysql_query($query);
> if ($result) {
> $howManyWeblogEntries = mysql_num_rows($result);
> for ($i=0; $i < $howManyWeblogEntries; $i++) {
> $row = getRow($result);
> if (is_array($row)) {
> extract($row);
> echo "<h1>$headline</h1>";
> echo "<h7>$date</h7>";
> echo "<div class=\"mainContent\">$mainContent</div>";
> } else {
> reportError("In getWeblogEntries, the function getRow
> failed to return an array on the $i iteration.");
> }
> }
> } else {
> reportError("In getWeblogEntries, the query to the database
> failed.");
> }
> }
<snip>
As someone has already mentioned, it would be better to capture
the errors via custom error handler which should clear all the texts
that are ready for output but to show the friendly message something
like "Script error occured, please retry later" on any fatal errors.
The error level can be triggered via trigger_error(). All errors can be
logged in a log file for the programmer to look into that later (but
the same error message shouldn't be shown to user), FWIW,
<http://groups.google.com/group/alt.comp.lang.php/msg/af1455b3c817fc0f>
Out of interest, if the script shown above is not intended to be
the example, it has some pitfalls: 1. selecting all rows is kind of
evil, 2. extract, is_array, etc are overkill.
--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/
Navigation:
[Reply to this message]
|