定義和用法
這個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()用法及代碼示例
- PHP mysqli_get_server_info()用法及代碼示例
- PHP mysqli_data_seek()用法及代碼示例
- PHP mysqli_insert_id()用法及代碼示例
- PHP mysqli_fetch_assoc()用法及代碼示例
- PHP mysqli_connect_error()用法及代碼示例
- PHP mysqli_fetch_all()用法及代碼示例
- PHP mysqli_next_result()用法及代碼示例
- PHP mysqli_stmt_affected_rows()用法及代碼示例
- PHP mysqli_begin_transaction()用法及代碼示例
- PHP mysqli_real_escape_string()用法及代碼示例
- PHP mysqli_fetch_lengths()用法及代碼示例
- PHP mysqli_character_set_name()用法及代碼示例
- PHP mysqli_free_result()用法及代碼示例
- PHP mysqli_fetch_row()用法及代碼示例
- PHP mysqli_debug()用法及代碼示例
- PHP mysqli_commit()用法及代碼示例
- PHP mysqli_fetch_field()用法及代碼示例
- PHP mysqli_kill()用法及代碼示例
- PHP mysqli_thread_safe()用法及代碼示例
注:本文由純淨天空篩選整理自 PHP mysqli_error_list() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。