ConFoo Montreal 2026: Call for Papers

Voting

: two minus two?
(Example: nine)

The Note You're Voting On

callforeach at gmail dot com
10 years ago
I had to set mysqli_report(MYSQLI_REPORT_ALL) at the begin of my script to be able to catch mysqli errors within the catch block of my php code.

Initially, I used the below code to throw and subsequent catch mysqli exceptions

<?php
try {
$mysqli = new mysqli('localhost','root','pwd','db');
if (
$mysqli->connect_errno)
throw new
Exception($mysqli->connect_error);

} catch (
Exception $e) {
echo
$e->getMessage();
}

I realized the exception was being thrown before the actual throw statement and hence the catch block was not being called.

My current code looks like
mysqli_report
(MYSQLI_REPORT_ALL) ;
try {
$mysqli = new mysqli('localhost','root','pwd','db');
/* I don't need to throw the exception, it's being thrown automatically */

} catch (Exception $e) {
echo
$e->getMessage();
}

This works fine and I'm able to trap all mysqli errors

<< Back to user notes page

To Top