English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_stmt_sqlstate() Function Usage and Example

PHP MySQLi Reference Manual

The mysqli_stmt_sqlstate() function returns the SQLSTATE error from the last statement operation

Definition and usage

mysqli_stmt_sqlstate()The function returns the SQLSTATE error that occurred during the execution of the last statement.

Syntax

mysqli_stmt_sqlstate($stmt);

Parameter

Serial numberParameters and descriptions
1

stmt(essential)

This is an object representing the statement (already prepared).

Return value

The PHP mysqli_stmt_sqlstate() function returns a string value that represents the SQLSTATE error that occurred during the execution of the last statement. If there is no error, this function returns00000.

PHP version

This function was originally introduced in PHP version5introduced and can be used in all higher versions.

Online example

The following example demonstratesmysqli_stmt_sqlstate()Usage of function (procedural style)-

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   mysqli_query($con, 'CREATE TABLE emp(ID INT, FirstName VARCHAR(20))");
   print("Create table.....\n");
   $stmt = mysqli_prepare($con, "INSERT INTO emp (ID) VALUES (?)");
   mysqli_stmt_bind_param($stmt, "s", $id);
   $id = 'test';
   //Execute statement
   mysqli_stmt_execute($stmt);
   //State
   $state = mysqli_stmt_sqlstate($stmt);
   print("state: " . $state);
   //End statement
   mysqli_stmt_close($stmt);
   //Close connection
   mysqli_close($con);
?>

Output result

Create table.....
state: HY000

Online example

In the object-oriented style, the syntax of this function is$con-> sqlstateHere is an example of this function in an object-oriented style-

<?php
   //Establish connection
   $con = new mysqli("localhost", "root", "password", "mydb");
   $con -> query("CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
   print("Create table.....\n");
   $con -> query("INSERT INTO myplayers values(")1, 'Sikhar', 'Dhawan', 'Delhi', 'India')");
   print("Insert record.....\n");
   $stmt = $con ->prepare("SELECT * FROM myplayers");
   $con ->query("DROP TABLE myplayers");
   //Execute statement
   $stmt->execute();
   //SQl State
   $state = $stmt ->sqlstate;
   print("SQL State: " . $state);
   //End statement
   $stmt->close();
   //Close connection
   $con->close();
?>

Output result

Create table.....
Insert record.....
SQL State: 42S02

Online example

Here ismysqli_stmt_sqlstate()Another example of the function-

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   mysqli_query($con, 'CREATE TABLE emp(ID INT, FirstName VARCHAR(20), LastName VARCHAR(5), DateOfBirth VARCHAR(255), Salary INT)');
   print("Create table.....\n");
   $stmt = mysqli_prepare($con, 'INSERT INTO emp values(?, ?, ?, ?, ?');
   mysqli_stmt_bind_param($stmt, 'isssi', $id, $fname, $lname, $pob, $country);
   $id = 1;
   $fname = 'Swetha';
   $lname = 'Yellapragada';
   $dob = DATE('1981-12-05);
   $country = 2366;
   //Execute statement
   mysqli_stmt_execute($stmt);
   //Warnings
   $state = mysqli_stmt_sqlstate($stmt);
   print("state: " . $state);
   //End statement
   mysqli_stmt_close($stmt);
   //Close connection
   mysqli_close($con);
?>

Output result

Create table.....
state: 22001

PHP MySQLi Reference Manual