Page 1 of 1

Handle Errors

Posted: Fri Nov 25, 2005 4:42 pm
by scts102
Hey guys,

I have been learning bits of php here and there, and I was wondering how to handle errors. For example , if I were to do a sql query (That returned an empty set), and assign variables to results in specific fields, how would I display my own error message, rather than mysql_result(): unable to jump to row 0.....?

Posted: Fri Nov 25, 2005 4:55 pm
by John Cartwright
http://ca.php.net/manual/en/function.se ... andler.php may be some use to you
although if your wanting to simply add error handler functionality to that particular class just customize a function to output it to whatever you like.

is..

Code: Select all

class database {
   function query($sql) {
      $result = @mysql_query($sql) or $this->error_function(mysql_error());
   }
   
   function error_function($error) {
      exit('<span style="font-color: red">ERROR! '.$error.'</span>');
   }
}
After re-reading your post I kind of see that your logic is off and your not particularly looking at an error handler. To avoid the situation your in, you would use mysql_num_rows($result); (assuming $result holds the rescource id)

for example

Code: Select all

$result = mysql_query($sql) or die(mysql_error());
   
   if (mysql_num_rows($result) > 0) {
      while ($row = mysql_fetch_assoc($result)) {
         $name = $row['name'];
      }
   }
   else {
      echo 'No rows found';
   }
If I still missed your point, please clarify.

Posted: Fri Nov 25, 2005 5:04 pm
by scts102
Yes, you were right in thinking my logic is off. Your second suggestion is what I was looking for.

Thank you