mysqli_error_list()函數(shù)返回最近調(diào)用函數(shù)的錯(cuò)誤列表。
mysqli_error_list()函數(shù)返回最近一次 MySQLi 函數(shù)調(diào)用過(guò)程中發(fā)生的錯(cuò)誤列表。
mysqli_error_list($con)
| 序號(hào) | 參數(shù)及說(shuō)明 |
|---|---|
| 1 | con(必需) 這是一個(gè)表示與MySQL Server的連接的對(duì)象。 |
PHP mysqli_error_list()函數(shù)返回一個(gè)列表,該列表表示執(zhí)行l(wèi)ast語(yǔ)句期間的錯(cuò)誤(每個(gè)錯(cuò)誤作為數(shù)組)。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_error_list()函數(shù)的用法(面向過(guò)程風(fēng)格)-
<?php
//建立連接
$con = mysqli_connect("localhost", "root", "password", "mydb");
mysqli_query($con, "CREATE TABLE Test(Name VARCHAR(10), AGE INT)");
//執(zhí)行查詢
$query = "INSERT into Test values('Raju', 25),('Rahman', 30),('Sri Rama Chandra Murthi', 25)";
mysqli_query($con, $query);
//錯(cuò)誤
$list = mysqli_error_list($con);
print_r($list);
//關(guān)閉連接
mysqli_close($con);
?>輸出結(jié)果
Array ( [0] => Array ( [errno] => 1406 [sqlstate] => 22001 [error] => Data too long for column 'Name' at row 3 ) )
在面向?qū)ο蟮臉邮街?,此函?shù)的語(yǔ)法為$con->error_list。以下是面向?qū)ο髽邮降拇撕瘮?shù)的示例-
<?php
//建立連接
$con = new mysqli("localhost", "root", "password", "mydb");
//查詢以檢索employee表的所有行
$con -> query("SELECT * FROM wrong_table_name");
//Error
$list = $con->error_list;
print_r($list);
//關(guān)閉連接
$con -> close();
?>輸出結(jié)果
Array ( [0] => Array ( [errno] => 1146 [sqlstate] => 42S02 [error] => Table 'mydb.wrong_table_name' doesn't exist ) )
假設(shè)我們有一個(gè)名為employee的表,其內(nèi)容如下:
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()函數(shù)的另一個(gè)示例-
<?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);
//關(guān)閉連接
mysqli_close($con);
?>輸出結(jié)果
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 ) )