Function name: mysqli_stmt::$errno()
Applicable version: PHP 5, PHP 7
Function Description: This function is used to get the error number of the most recently executed preprocessing statement (mysqli_stmt).
usage:
public mysqli_stmt::errno ( void ) : int
Parameter description: No parameters.
Return value: Returns an integer indicating the error number of the most recently executed preprocessing statement. If no error occurs, return 0.
Example:
// 创建数据库连接$mysqli = new mysqli("localhost", "username", "password", "database"); // 检查连接是否成功if ($mysqli->connect_errno) { die("连接失败: " . $mysqli->connect_error); } // 创建预处理语句$stmt = $mysqli->prepare("SELECT * FROM users WHERE id = ?"); // 绑定参数$id = 1; $stmt->bind_param("i", $id); // 执行预处理语句$stmt->execute(); // 获取错误号码$errorCode = $stmt->errno(); // 检查是否有错误发生if ($errorCode !== 0) { echo "执行预处理语句时发生错误,错误号码:" . $errorCode; } else { echo "预处理语句执行成功!"; } // 关闭预处理语句和数据库连接$stmt->close(); $mysqli->close();
In the above example, we first create a database connection and check if the connection is successful. We then create a preprocessing statement, bind a parameter, and execute the preprocessing statement. Next, we use the $stmt->errno()
function to get the error number of the most recently executed preprocessing statement. If the error number is not 0, it means that an error occurred during the execution of the preprocessing statement, and we can perform corresponding processing based on the error number. If the error number is 0, it means that the preprocessing statement is executed successfully. Finally, we closed the preprocessing statement and the database connection.
Note that the $stmt->errno()
function needs to be called after the preprocessing statement is executed, otherwise 0 will be returned.