mysqli异常类

(PHP 5, PHP 7)

简介

mysqli异常类

类摘要

mysqli_sql_exception extends RuntimeException {
/* 属性 */
protected string $sqlstate ;
/* 继承的属性 */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
}

属性

sqlstate

出现错误的sql状态

User Contributed Notes

cnlyzy at qq dot com 12-May-2017 05:11
<?php
mysqli_report
(MYSQLI_REPORT_STRICT);
try {
   
$mysqli = new mysqli('127.0.0.1','uesr','password','testDB');
    echo
'connect success';
} catch (
Exception $e) {
    echo
'ERROR:'.$e->getMessage();
}
fibrizo dot raziel at gmail dot com 03-Nov-2015 12:34
<?php  mysqli_report(MYSQLI_REPORT_STRICT) ?>
was not enough for me to enable exception throwing. I had to write this:
<?php mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); ?>
callforeach at gmail dot com 10-Mar-2015 10:42
Please note that MYSQLI_REPORT_STRICT does not allow certain exceptions to be trapped. I find MYSQLI_REPORT_ALL to be more friendly since it allows me to trap all errors and handle them appropriately.
polyanin at gmail dot com 19-Feb-2014 03:51
$driver = new mysqli_driver();
        $driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
dronebraindeveloper at gmail dot com 01-Jan-2013 10:37
Quick note on how to setup and use mysqli_sql_exceptions properly

<?php
define
("MYSQL_CONN_ERROR", "Unable to connect to database.");

// Ensure reporting is setup correctly
mysqli_report(MYSQLI_REPORT_STRICT);

// Connect function for database access
function connect($usr,$pw,$db,$host) {
   try {
     
$mysqli = new mysqli($host,$usr,$pw,$db);
     
$connected = true;
   } catch (
mysqli_sql_exception $e) {
      throw
$e;
   }
}

try {
 
connect('username','password','database','host');
  echo
'Connected to database';
} catch (
Exception $e) {
  echo
$e->errorMessage();
}
?>