Handle Errors

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scts102
Forum Newbie
Posts: 23
Joined: Thu Aug 25, 2005 2:15 pm

Handle Errors

Post 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.....?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
scts102
Forum Newbie
Posts: 23
Joined: Thu Aug 25, 2005 2:15 pm

Post by scts102 »

Yes, you were right in thinking my logic is off. Your second suggestion is what I was looking for.

Thank you
Post Reply