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.....?
Handle Errors
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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..
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
If I still missed your point, please clarify.
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>');
}
}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';
}