php教程

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 08: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 02-Nov-2015 03: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 01: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 18-Feb-2014 06:51
$driver = new mysqli_driver();
        $driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
dronebraindeveloper at gmail dot com 01-Jan-2013 01: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();
}
?>

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788