I started using OO-MySQLi after procedural MySQL and I have a problem.
In production environment my system displays all errors as a custom page. MySQLi errors is an "error" too and I want catch them, but in documentation described only one way to do this:
if (!$mysqli->query("SET a=1")) {
exit('An error occurred: ' . $mysqli->error);
}
(just for example). This is a very bad way, because in my system I'm doing many things when error occurred, like logging, calling events, etc.
Of course, I can extend mysqli class, for example:
class myMysqli {
public function __construct(/* ... */)
{
parent::__construct(/* ... */);
}
public function query(/* .. */)
{
parent::query(/* ... */);
if($this->errno !== 0)
{
// An error occurred
}
}
}
$mysqli = new myMysqli(/* ... */);
$mysqli->query(/* ... */);
But this way I need to extend almost ALL methods where error can occur. Moreover, MySQLi provides prepared statements (mysqli_stmt class) that has its own errors!
Can you know a better way to handle MySQLi errors? Thank you in advance.
Added
About exceptions: Do I understand correctly that with exceptions I need do something like this:
try {
$mysqli->query(/* ... */);
} catch (Exception $e) {
// An error occurred
}
But this is similar to
if(!$mysqli->query(/* ... */))
{
// An error occured
}
What a difference?