编辑这个页面须要登录或更高权限!
mysqli_error_list()函数返回最近调用函数的错误列表。
mysqli_error_list()函数返回最近一次 MySQLi 函数调用过程中发生的错误列表。
mysqli_error_list($con)
序号 | 参数及说明 |
---|---|
1 | con(必需) 这是一个表示与MySQL Server的连接的对象。 |
PHP mysqli_error_list()函数返回一个列表,该列表表示执行last语句期间的错误(每个错误作为数组)。
此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_error_list()函数的用法(面向过程风格)-
<?php //建立连接 $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE Test(Name VARCHAR(10), AGE INT)"); //执行查询 $query = "INSERT into Test values('Raju', 25),('Rahman', 30),('Sri Rama Chandra Murthi', 25)"; mysqli_query($con, $query); //错误 $list = mysqli_error_list($con); print_r($list); //关闭连接 mysqli_close($con); ?>
输出结果
Array ( [0] => Array ( [errno] => 1406 [sqlstate] => 22001 [error] => Data too long for column 'Name' at row 3 ) )
在面向对象的样式中,此函数的语法为$con->error_list。以下是面向对象样式的此函数的示例-
<?php //建立连接 $con = new mysqli("localhost", "root", "password", "mydb"); //查询以检索employee表的所有行 $con -> query("SELECT * FROM wrong_table_name"); //Error $list = $con->error_list; print_r($list); //关闭连接 $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 //建立连接 $con = mysqli_connect("localhost", "root", "password", "mydb"); //查询以选择employee表的所有行 mysqli_query($con, "SELECT * FROM employee"); $list = mysqli_error_list($con); print_r($list); //用于更新Employee表的行的查询 mysqli_query($con, "UPDATE employee set INCOME=INCOME+5000 where FIRST_NAME in (*)"); $list = mysqli_error_list($con); print_r($list); //查询以将一行插入到employee表中 mysqli_query($con, "INSERT INTO employee VALUES (Archana, 'Mohonthy', 30, 'M', 13000, 106)"); $list = mysqli_error_list($con); print_r($list); //关闭连接 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 ) )