tags:

views:

28

answers:

2

I'm using $mysqli->query all over the place without any sort of error checking or fall back if the query fails.

What's the simplest/cleanest method I can use to catch these ?

I'm thinking of a little function which can just display a friendly message and send me an email - I can write that but am not sure how to trigger/call it only when an error occurs? without adding heaps of if statements...

i.e I wrap them all like:

if($mysqli->query($query)) blah } else { error }

That will really bloat my code...

+1  A: 

Make your own database class that extends mysqli, and does the error catching/emailing when you call my_mysqli->doQuery():

class my_mysqli extends mysqli
{
  function doQuery($query)
  {
    if($this->query($query)) return $blah } else { do error blah }
  }
}
fredley
That's pretty cool. Thanks!What other useful things would you do with that class?
dinosaur
anything you want!
fredley
A: 

This doesn't seem to work:

$db = new db('localhost', 'user', 'pass', 'mydb');


class db extends mysqli{

   function do_query($query) { 

    if(  $result = parent::query( $query ) ){

        return $result;

    } else { 
        var_dump($result->error);

    }
}

}

but i don't get an error either, just NULL / 0

dinosaur
never mind - had a connection issue... sorted!
dinosaur
though I don't seem to be getting any errors displayed at all if I intentionally screw up the query ??
dinosaur
You can't dump `$result->error` because `$result` is `FALSE` on a failure. Try `$this->error` instead.
grossvogel