(PHP 5, PHP 7, PHP 8)
mysqli_stmt::$error -- mysqli_stmt_error — 返回上次语句错误的字符串描述
面向对象风格
过程式风格
返回一个字符串,其中包含最近调用的可能成功或失败的语句函数的错误消息。
描述错误的字符串。如果没有发生错误,则为空字符串。
示例 #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();
printf("Error: %s.\n", $stmt->error);
/* 关闭语句 */
$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);
printf("Error: %s.\n", mysqli_stmt_error($stmt));
/* 关闭语句 */
mysqli_stmt_close($stmt);
}
/* 关闭连接 */
mysqli_close($link);
?>
上面的示例将输出
Error: Table 'world.myCountry' doesn't exist.