当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP mysqli_error_list()用法及代码示例



定义和用法

这个mysqli_error_list()函数返回上次 MySQLi 函数调用期间发生的错误列表。

用法

mysqli_error_list($con)

参数

Sr.No 参数及说明
1

con(Mandatory)

这是一个表示与 MySQL 服务器的连接的对象。

返回值

PHP mysqli_error_list() 函数返回一个列表,表示执行最后一条语句期间的错误(每个错误作为一个数组)。

PHP版本

这个函数最初是在 PHP 版本 5 中引入的,适用于所有后续版本。

示例

以下示例演示了 mysqli_error_list() 函数的用法(程序风格) -

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   mysqli_query($con, "CREATE TABLE Test(Name VARCHAR(10), AGE INT)");


   //Executing the query
   $query = "INSERT into Test values('Raju', 25),('Rahman', 30),('Sri Rama Chandra Murthi', 25)";
   mysqli_query($con, $query);

   //Error
   $list = mysqli_error_list($con);
   print_r($list);

   //Closing the connection
   mysqli_close($con);
?>

这将产生以下结果 -

Array
(
    [0] => Array
        (
            [errno] => 1406
            [sqlstate] => 22001
            [error] => Data too long for column 'Name' at row 3
        )

)

示例

在面向对象风格中,这个函数的语法是 $con ->error_list。以下是面向对象风格的此函数的示例 -

<?php
   //Creating a connection
   $con = new mysqli("localhost", "root", "password", "mydb");

   //Query to retrieve all the rows of employee table
   $con -> query("SELECT * FROM wrong_table_name");
  
   //Error 
   $list = $con->error_list;

   print_r($list);

   //Closing the connection
   $con -> close();
?>

这将产生以下结果 -

Array
(
    [0] => Array
        (
            [errno] => 1146
            [sqlstate] => 42S02
            [error] => Table 'mydb.wrong_table_name' doesn't exist
        )

)

示例

假设我们有一个名为employee 的表,其内容如下 -

mysql> select * from employee;
+------------+--------------+------+------+--------+
| FIRST_NAME | LAST_NAME    | AGE  | SEX  | INCOME |
+------------+--------------+------+------+--------+
| Vinay      | Bhattacharya |   20 | M    |  16000 |
| Sharukh    | Sheik        |   25 | M    |  13300 |
| Trupthi    | Mishra       |   24 | F    |  31000 |
| Sheldon    | Cooper       |   25 | M    |   2256 |
| Sarmista   | Sharma       |   28 | F    |  15000 |
+------------+--------------+------+------+--------+
5 rows in set (0.06 sec)

以下是 mysqli_error_list() 函数的另一个示例 -

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   //Query to SELECT all the rows of the employee table
   mysqli_query($con, "SELECT * FROM employee");
   $list = mysqli_error_list($con);
   print_r($list);

   //Query to UPDATE the rows of the employee table
   mysqli_query($con, "UPDATE employee set INCOME=INCOME+5000 where FIRST_NAME in (*)");
   $list = mysqli_error_list($con);
   print_r($list);

   //Query to INSERT a row into the employee table
   mysqli_query($con, "INSERT INTO employee VALUES (Archana, 'Mohonthy', 30, 'M', 13000, 106)");
   $list = mysqli_error_list($con);
   print_r($list);

   //Closing the connection
   mysqli_close($con);
?>

这将产生以下结果 -

Array
(
)
Array
(
    [0] => Array
        (
            [errno] => 1064
            [sqlstate] => 42000
            [error] => 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 '*)' at line 1
        )

)
Array
(
    [0] => Array
        (
            [errno] => 1136
            [sqlstate] => 21S01
            [error] => Column count doesn't match value count at row 1
        )

)

相关用法


注:本文由纯净天空筛选整理自 PHP mysqli_error_list() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。