30

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:

public function mysqlQuery($SQL) {

    $this->Result = mysqli_query($this->DBlink, $SQL);

    if($this->Result === false)
        throw new MySQLiQueryException($SQL, mysqli_error($this->DBlink), mysqli_errno($this->DBlink));

    return $this->Result;

}

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

Roman Newaza
  • 11,405
  • 11
  • 58
  • 89

3 Answers3

44

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

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 3
    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. – Roman Newaza Jan 29 '13 at 08:38
  • 2
    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. – Karoly Horvath Jan 29 '13 at 08:42
  • The link for PHP error is down instead is [this](https://phpdelusions.net/pdo#errors) – Simone Rossaini May 20 '21 at 09:16
28

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).

public function mysqlQuery($SQL) {
    try {
        $this->Result = mysqli_query($this->DBlink, $SQL);
    } catch (mysqli_sql_exception $e) {
        throw new MySQLiQueryException($SQL, $e->getMessage(), $e->getCode());
    }
    return $this->Result;
}

(NB: I changed $this->SQL to $SQL in the re-thrown exception.)

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • 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? – Louis Loudog Trottier May 12 '17 at 05:18
  • 1
    @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. – MrWhite May 12 '17 at 09:07
3

I know it's a little too late, but for the sake of posterity. I find MYSQLI_REPORT_STRICT to be to restrictive, certain exceptions where not be raised and hence they could not be handled by the catch block.

 mysqli_report(MYSQLI_REPORT_ALL); // Traps all mysqli error 

 try {
    $mysqli = new mysqli('localhost','user,'pwd','db');

     /* I don't need to explicitly throw an exception as this is being
      done automatically */

 } catch(Exception $e) {
    echo $e->getMessage();
 }  
st__gen_server
  • 597
  • 5
  • 9
  • `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);`? – Louis Loudog Trottier May 12 '17 at 05:16
  • 1
    @LouisLogTrottier `Exception` object is the parent of all exceptions (`mysqli_sql_exception` is a subclass) so it will naturally catch _everything_. – MrWhite May 12 '17 at 09:14
  • 2
    `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. – Your Common Sense May 05 '20 at 10:11