32

I'm trying to turn MySQLi query errors to Exceptions, but couldn't - mysqli_sql_exception is thrown only if it failed to connect the DB.

I used mysqli_report(MYSQLI_REPORT_STRICT) and procedural MySQLi functions embedded to custom wrapper class.

Former code:

$result = mysqli_query($DBlink, $SQL);
if($result === false) {
    throw new MySQLiQueryException($SQL, mysqli_error($DBlink), mysqli_errno($DBlink));
}

Question: Is it normal no Warning, nor Exception are thrown when query fails so I have to check if mysqli_query() returned false?

4
  • 1
    As far as I know mysqli only throws exception only on DB connect. So ... pretty much, yes. You have mysqli_errno, mysqli_error and other functions so you know the error, but that's the limit. Commented Jan 29, 2013 at 8:12
  • You can throw exceptions if sometning goes wrong with just try, catch, throw Commented Jan 29, 2013 at 8:14
  • @k102, I was trying to put that part to MySQLi from my code. Commented Jan 29, 2013 at 8:32
  • @VladPreda I see, seams like I have to leave it as it is now. Commented Jan 29, 2013 at 8:44

3 Answers 3

46

Some time ago I managed to sort this matter out. As it was pointed out in the other answer,

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

is a correct way to tell mysqli to throw exceptions.

Just make sure you don't wrap every query in a try-catch. This is a very common misconception that as soon as you started using exceptions you should start throwing tries and catches left and right. Quite contrary, try-catch should be used warily. While 99% of your errors shouldn't be handled in place, but rather by a site-wide error handler. You may read more on the topic from my article on PHP error reporting

Sign up to request clarification or add additional context in comments.

3 Comments

This is how it is now. I'm asking if it is possible to instruct MySQLi driver to throw Exceptions, the same as you do with PDO.
Yes, your question is clear. But it's (a very) good practice to use a wrapper function to execute queries, once you have that, it's trivial to add those extra two lines.
The link for PHP error is down instead is this
29

do I have to check if mysqli_query() returned false?

No.

You should be able to do what you require and instruct the mysqli driver to throw exceptions on SQL errors, but you will need to enable MYSQLI_REPORT_ERROR if it is not already....

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT)

mysqli_query() should now throw exceptions on error. You do not need to check the return value for failure (which won't happen anyway because an exception is thrown). Hence just execute your query right away

$result = mysqli_query($DBlink, $SQL);

and mysqli_sql_exception will be thrown by PHP.

2 Comments

Is ti the fact that you are using mysqli_sql_exception` that allow the use of method $e->getCode()`, and that will reflect the $mysqli->errno i guess?
@LouisLoudogTrottier Yes, getCode() is a method of the mysqli_sql_exception object, so is only available in the scope of the catch block of that exception type.
4

I know it's a little too late, but for the sake of posterity. I find that MYSQLI_REPORT_ERROR doesn't report all possible errors. There is an option in mysqli that allow MySQL to report an error if no index or bad index was used. If you want to convert such warnings into exceptions too, add MYSQLI_REPORT_INDEX option to mysqli_report() (or simply use MYSQLI_REPORT_ALL as it includes all possible options)

<?php
mysqli_report(MYSQLI_REPORT_ALL); // Use all options including index warnings  
 
/* I don't need to explicitly throw an exception as this is being
  done automatically */
$mysqli = new mysqli('localhost','user,'pwd','db');
$mysqli->query("bogus")

will output something like

PHP Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'bogus' at line 1 in mysqli.php:12
Stack trace:
#0 mysqli.php(12): mysqli->query('bogus')
#1 {main}
thrown in mysqli.php on line 12

6 Comments

Exception $e will catch mysqli failure by itself or do we need to use mysqli_sql_exception $e? or are they equivalent? or is Exception used because of mysqli_report(MYSQLI_REPORT_ALL);`?
@LouisLogTrottier Exception object is the parent of all exceptions (mysqli_sql_exception is a subclass) so it will naturally catch everything.
catch(Exception $e) { echo $e->getMessage(); is a useless cargo cult code. If you take it out, the outcome will be the same: PHP reports uncaught exceptions already.
@YourCommonSense - possible to share sample code ? I use $e->getMessage() inside trigger_error to get a message in the apache error log. Do u have a better way ? Pls share.
@MarcoZen there is no such code. Just remove all try catch altogether. An exception is the same error as one you create with trigger_error. You are doing a lot of work (try, catch, trigger error) for something that PHP ALREADY DOES
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.