(PHP 5, PHP 7, PHP 8)
mysqli_stmt::$errno -- mysqli_stmt_errno — 返回最近语句调用返回的错误代码
面向对象风格
过程式风格
返回最近调用的语句函数的错误代码,该函数可能成功或失败。
一个错误代码值。零表示没有发生错误。
示例 #1 面向对象风格
<?php
/* 打开连接 */
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* 检查连接 */
if (mysqli_connect_errno()) {
printf("连接失败:%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("错误:%d.\n", $stmt->errno);
/* 关闭语句 */
$stmt->close();
}
/* 关闭连接 */
$mysqli->close();
?>
示例 #2 过程式风格
<?php
/* 打开连接 */
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* 检查连接 */
if (mysqli_connect_errno()) {
printf("连接失败:%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("错误:%d.\n", mysqli_stmt_errno($stmt));
/* 关闭语句 */
mysqli_stmt_close($stmt);
}
/* 关闭连接 */
mysqli_close($link);
?>
上面的示例将输出
Error: 1146.