English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_sqlstate() function returns the last SQLSTATE error code of the last error.
mysqli_sqlstate()The function returns a string containing the SQLSTATE error code of the last SQL operation. Error codes are defined by 5 characters constitute, '00000' indicates no error has occurred. Error codes are defined by ANSI SQL and ODBC.
Note: It should be noted that not all MySQL errors are mapped to SQLSTATE, unmap errors are represented by HY000 (composite error).
mysqli_sqlstate($con)
Serial number | Parameters and descriptions |
---|---|
1 | con(Required) This is an object representing the connection to the MySQL Server. |
The PHP mysqli_sqlstate() function returns a string value that represents the SQLSTATE error that occurred during the last MySQL operation. If no error occurs, this function returns00000.
This function was initially introduced in PHP version5introduced and can be used in all higher versions.
The following example demonstratesmysqli_sqlstate()Usage of the function (procedural style)-
<?php //Establish connection $con = mysqli_connect("localhost", "root", "password", "mydb"); //Query to retrieve all records from the table mysqli_query($con, "Select * from WrongTable"); //SQL State $state = mysqli_sqlstate($con); print("SQL state error: ",$state); //Close connection mysqli_close($con); ?>
Output result
SQL state error: 42S02
In object-oriented style, the syntax of this function is$con-> sqlstate. The following is an example of this function in object-oriented style-
<?php //Establish connection $con = new mysqli("localhost", "root", "password", "mydb"); //Query to retrieve all records from the employee table $con -> query("Select FIRST_NAME, LAST_NAME, AGE from employee"); //SQL State $state = $con-> sqlstate; print("SQL state error: ",$state); //Close connection $con -> close(); ?>
Output result
SQL state error: 42000
The following ismysqli_sqlstate()Another example of a function-
<?php //Establish connection $con = mysqli_connect("localhost", "root", "password", "mydb"); //Query to select all rows from the employee table mysqli_query($con, "SELECT * FROM employee"); print("SQL State Error: ". mysqli_sqlstate($con) . "\n"); //Query to update rows in the employee table mysqli_query($con, "UPDATE employee SET INCOME=INCOME+5000 WHERE FIRST_NAME IN (*); print("SQL State Error: ". mysqli_sqlstate($con) . "\n"); //Query to insert a row into the employee table mysqli_query($con, "INSERT INTO employee VALUES (Archana, 'Mohonthy', 30, 'M', 13000, 106); print("SQL State Error: ". mysqli_sqlstate($con) . "\n"); //Close connection mysqli_close($con); ?>
Output result
SQL State Error: 00000 SQL State Error: 42000 SQL State Error: 42S22
Returns the SQLSTATE error code of the last MySQL operation:
<?php $connection_mysql = mysqli_connect("localhost", "root", "password", "mydb"); if (mysqli_connect_errno($connection_mysql)){ echo "Connection to MySQL failed: ". mysqli_connect_error(); } //Assuming we already have a table named Persons in the database mydb $sql = "CREATE TABLE Persons (Firstname VARCHAR(30), Lastname VARCHAR(30), Age INT)"; if (!mysqli_query($connection_mysql, $sql)){ echo "SQLSTATE error: ". mysqli_sqlstate($connection_mysql); } mysqli_close($connection_mysql); ?>
Output result
SQLSTATE error: 42S01