Current Location: Home> Function Categories> mysqli_stmt::$sqlstate

mysqli_stmt::$sqlstate

(mysqli_stmt_sqlstate) Returns SQLSTATE error from previous statement operation
Name:mysqli_stmt::$sqlstate
Category:MySQLi
Programming Language:php
One-line Description:Get the SQL status code for the last statement execution

The mysqli_stmt::$sqlstate() function is used to obtain the SQL status code of the last statement execution.

usage:

 string mysqli_stmt::$sqlstate();

Parameters: This function has no parameters.

Return value: This function returns a string representing the SQL status code.

Example:

 // 创建数据库连接$mysqli = new mysqli("localhost", "username", "password", "database"); // 准备SQL语句$stmt = $mysqli->prepare("SELECT * FROM users WHERE age > ?"); // 绑定参数$age = 18; $stmt->bind_param("i", $age); // 执行查询$stmt->execute(); // 获取SQL状态码$sqlstate = $stmt->sqlstate(); if ($sqlstate == '00000') { echo "查询成功!"; } else { echo "查询失败,错误码:" . $sqlstate; } // 关闭语句和数据库连接$stmt->close(); $mysqli->close();

In the example above, we first create a database connection and then prepare a SQL statement with parameters using the prepare() method. Next, we use the bind_param() method to bind a parameter and then execute the query. Finally, we use the sqlstate() method to obtain the SQL status code of the last query and output different messages according to different status codes.

It should be noted that the SQL status code is a five-digit string that starts with a number to represent different SQL execution status. In the example, we determine whether the status code is '00000', indicating that the query is successful. If the status code is not '00000', it means that the query has failed and error processing can be performed according to the specific status code.

Similar Functions
Popular Articles