Jump to content

Advice on database exceptions/errors


franko75

Recommended Posts

Hi, I'm just looking for a bit of advice about how best to handle database errors - mainly, when to use exceptions and when to use error messages/codes. If i have a database class which i use to perform a query/update/insert/delete - how would you handle any errors by the queries failing?

 

for example, i could do this:

public function query($sql)
    {
        
        if ($sql)
	{
		return mysql_query($sql);
	}
	else 
	{
		$this->error = mysql_error();
		return FALSE;
	}
    }

and call it with:

$db = new Database;
$query = $db->query("SELECT id, title, content, item_date, date_posted FROM news where status = 1");

Is it good practice then to check for errors for every query you perform, like this:

if ( ! $query)
$error = $db->error;
//stop processing and show message to user
else //continue processing

 

I''m interested in finding out how other people handle this. Previously I used procedural php and didn't really check properly for errors in these types of situation (not good practice i know!).

Link to comment
Share on other sites

I use my DB class to handle the errors. In production I check mysql_error after every query, if there is an error I send an email to myself with the error and where it occured etc. Then display a nice page to the user "We are sorry an error was encountered, the admin has been notified."

 

If I am developing, I have it simply print the error to the page

 

public function query($sql)
    {
        
        if ($sql)
     {
        $resource = mysql_query($sql);
        if (mysql_error()) {
             return $this->error();
        }else {
             return $resource;
       }
}else 
{
$this->error = mysql_error();
return FALSE;
}
    }

 

Sorry bout the formating, copying from the [ php ] tags tends to do that.

 

A side note I was taught when I first started programming, handle the errors where they are thrown. So instead of handling them somewhere else in the code, handle them at the source and save yourself alot of extra coding.

Link to comment
Share on other sites

@premiso: thanks for the advice, i'll definitely do something similar for the development and in production stages.

 

In terms of catching errors where they are thrown, for system errors (e.g db connection issues) I am throwing exceptions, but for errors caused in sql queries etc, i am returning error messages with mysql_error() - does this seem a reasonable approach?

 

Also just realised my code wasn't correct in the first post - I've updated it using your snippet as follows:

public function query($sql)
{
        if ($sql)
        {
            $resource = mysql_query($sql);
            if (mysql_error())
            {
                $this->error = mysql_error();
                return FALSE;
            } 
else
            {
                return $resource;
            }
        } 
          else
        {
            $this->error = "No query supplied";
            return FALSE;
        }
    }

Link to comment
Share on other sites

imo, I have a function in the class called error(). When this is called it sends an email with the error information to me, if that is setup and then redirects the user to the page (session helps me keep track of pages the user was at), so nothing is returned. If I am doing it in development, I simply print the error to the screen, then die the script.

 

Cause if you get that error chances are the rest of the script is prone to errors etc due to that one single error, so why run it if you know the next errors to post cede it are potentially false errors?

 

At least that is my philosphy. Anytime I am using classes and I get an error, I always send it to the error function and depending what it is kill the script or do something else. Th email sent ensures that you know there is a problem, where it is and allows you to fix it without the user every seeing stuff they should not see in a production environment.

Link to comment
Share on other sites

Yeah that all makes sense, thanks again.

 

Re the error() function, i take it this could just be a private function inside the database class which you can pass an error to as a parameter (e.g mysql_error() or a custom error)?

 

Yep, except if you have $this->error setup, I would just use that and skip passing the parameter.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.