3

Is it possible to use an exception at the end of a mysql query instead of die()? I'd like to throw an exception and log it instead of killing the script.

Would it be done like:

mysql_query(...) or throw new exception()??
Bebo
  • 33
  • 1
  • 4

4 Answers4

1

This is the way I normally do it. I have my database in a wrapper class, so $this simply refers to the wrapper.

private function throwException($query = null)
{
    $msg = mysql_error().".  Query was:\n\n".$query.
                   "\n\nError number: ".mysql_errno();
    throw new Exception($msg);
}

public function query($query_string)
{
    $this->queryId = mysql_query($query_string);
    if (! $this->queryId) {
        $this->throwException($query_string);
    }
    return $this->queryId;
}

That packages it all up with a nice error message for me, so I can see the problem query. You could keep it much simpler of course, and do:

mysql_query($sql) or throw new Exception("Problem with query: ".$sql);
zombat
  • 92,731
  • 24
  • 156
  • 164
  • Zombat, thanks. This will help me I think. I'd just like to be able to see the errors if and when they occur. I'm tired of banging my head on a wall only to find out later that my query was wrong. – Bebo Jan 11 '10 at 21:55
  • Yeah that should help then. An uncaught Exception will function almost the same as a `die()`, but you can get a little more information into and out of Exceptions. – zombat Jan 11 '10 at 22:02
  • It is bad design to use a property of the surrounding object if `queryId` is only used within this method. And by the way: it would be better (for security and performance) to use prepared statements. – deamon Jan 12 '10 at 08:13
0

Yes. You can use an exception almost anywhere you'd traditionally use die/exit.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • This is incorrect. In fact, you can't even substitute is in this particular instance: http://stackoverflow.com/questions/1211237/php-or-statement-on-instruction-fail-how-to-throw-a-new-exception – Jeff Clemens Jun 05 '14 at 19:52
0

Yes, you can throw an exception instead of killing the script.

deamon
  • 89,107
  • 111
  • 320
  • 448
0

Your given code would work fine:

mysql_query(...) or throw new Exception("Failed to run query");

But it's using shorthand that is not entirely clear. I personally think that you're better off sacrificing brevity for clarity:

if (false === mysql_query(...)) {
    throw new Exception("Failed to run query.");
}

Keep in mind that throwing Exceptions is only useful if you catch them somewhere, and in your case you probably want to use a custom exception:

class DatabaseException extends Exception {}

try {
    if (false === mysql_query(...)) {
        throw new DatabaseException("Failed to run query.");
    }
} catch (DatabaseException $e) {
    $logger->log("Database exception: " . $e->getMessage());
}

Hope that helps!

inxilpro
  • 20,094
  • 2
  • 24
  • 28
  • Actually, this does help me. What I think I can do is just add something in my database class for an exception. This is pretty straight forward though. Thank you for this. – Bebo Jan 11 '10 at 22:07