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;
Just simply add or die(mysqli_error($db)); at the end of your query, this will print the mysqli error.
Definition and Usage The error / mysqli_error() function returns the last error description for the most recent function call, if any.
Description ¶ Instead, use mysql_error() to retrieve the error text. Note that this function only returns the error text from the most recently executed MySQL function (not including mysql_error() and mysql_errno()), so if you want to use it, make sure you check the value before calling another MySQL function.
The main advantage of PDO over MySQLi is in the database support. PDO supports 12 different database types, in opposition to MySQLi, which supports MySQL only. When you have to switch your project to use another database, PDO makes the process simpler.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With