phperror-handlingmysqlierror-log

MySQLi error handling


Is it possible to specify that MySQLi sends any errors and warnings to the PHP default 'error_log' directive? I can't seem to find any error options for the class specification, and I don't wish to handle errors manually like so:

if ($result = $mysqli->query("...")) {  }
else
    handle $mysqli->error;

Solution

  • Well, one way would be to override the class:

    class myMySQLi extends MySQLi {
    
        public function query($query, $resultmode = MYSQLI_STORE_RESULT) {
            $res = parent::query($query, $resultmode);
            if (!$res) {
                //handle error
            }
            return $res;
        }
    }
    

    Then just use as normal, except instead of creating an connection via new MySQLi(), use new myMySQLi(). Other than the error handling, it'll run just the same. I do this quite often, to throw exceptions on errors and to add additional functionality to MySQLi...