(PHP 5 >= 5.4.0, PHP 7, PHP 8)
mysqli_stmt::$error_list -- mysqli_stmt_error_list — 返回最后执行语句的错误列表
面向对象风格
过程式风格
返回最近调用的语句函数的错误数组,该函数可能成功或失败。
一个错误列表,每个错误都是一个包含 errno、error 和 sqlstate 的关联 array。
示例 #1 面向对象风格
<?php
/* 打开连接 */
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* 检查连接 */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$mysqli->query("CREATE TABLE myCountry LIKE Country");
$mysqli->query("INSERT INTO myCountry SELECT * FROM Country");
$query = "SELECT Name, Code FROM myCountry ORDER BY Name";
if ($stmt = $mysqli->prepare($query)) {
/* 删除表格 */
$mysqli->query("DROP TABLE myCountry");
/* 执行查询 */
$stmt->execute();
echo "Error:\n";
print_r($stmt->error_list);
/* 关闭语句 */
$stmt->close();
}
/* 关闭连接 */
$mysqli->close();
?>
示例 #2 过程式风格
<?php
/* 打开连接 */
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* 检查连接 */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
mysqli_query($link, "CREATE TABLE myCountry LIKE Country");
mysqli_query($link, "INSERT INTO myCountry SELECT * FROM Country");
$query = "SELECT Name, Code FROM myCountry ORDER BY Name";
if ($stmt = mysqli_prepare($link, $query)) {
/* 删除表格 */
mysqli_query($link, "DROP TABLE myCountry");
/* 执行查询 */
mysqli_stmt_execute($stmt);
echo "Error:\n";
print_r(mysql_stmt_error_list($stmt));
/* 关闭语句 */
mysqli_stmt_close($stmt);
}
/* 关闭连接 */
mysqli_close($link);
?>
以上示例将输出
Error: Array ( [0] => Array ( [errno] => 1146 [sqlstate] => 42S02 [error] => Table 'world.myCountry' doesn't exist ) )