HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
PHP mysqli Reference : Return the SQLSTATE error code for the last error
The sqlstate / mysqli_sqlstate() function returns the SQLSTATE error code for the last error.
The error code consists of five characters. "00000" indicates no error. The values are specified by ANSI SQL and ODBC.
Object oriented style:
Procedural style:
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | A string containing the SQLSTATE error code for the last error |
---|---|
PHP Version: | 5+ |
Return the SQLSTATE error code for the last error:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit;
}
// Table Persons already exists, so we should get an error
$sql = "CREATE TABLE Persons (Firstname VARCHAR(30), Lastname VARCHAR(30), Age INT)"
if (!mysqli_query($con, $sql)) {
echo "SQLSTATE error: ". mysqli_sqlstate($con);
}
// Close connection
mysqli_close($con);
?>
❮ PHP mysqli Reference