PHP | mysqli_sqlstate() Function

PHP mysqli_sqlstate() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_sqlstate() Function.

The built-in PHP mysqli_sqlstate() function returns the SQLSTATE error which occurred during the last MYSQLi function call.

Syntax

Following below is the syntax to use this function -

mysqli_sqlstate($con)


Parameter Details

Sr.NoParameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.


Return Value

This built-in PHP function returns a string value representing the SQLSTATE error that occurred during the last MYSQL operation. If no error occurred, this function returns 00000.

PHP Version

This PHP function was first introduced in PHP version 5 and works in all the later versions.

Example1

The following below is an example which shows the usage of PHP mysqli_sqlstate() function (in a procedural style) -

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   //Query to retrieve all the records of a table
   mysqli_query($con, "Select * from WrongTable");

   //SQL State
   $state = mysqli_sqlstate($con);
   print("SQL State Error: ".$state);

   //Closing the connection
   mysqli_close($con);
?>

Output

When the above code is executed, it will produce the following result -

SQL State Error: 42S02

Example2

In an object oriented style the syntax of this function is $con->sqlstate. Following is the example of this PHP function in an object oriented style -

<?php
   //Creating a connection
   $con = new mysqli("localhost", "root", "password", "mydb");

   //Query to retrieve all the records of the employee table
   $con -> query("Select FIRST_NAME, LAST_NAME, AGE form employee");

   //SQL State
   $state = $con->sqlstate;
   print("SQL State Error: ".$state);

   //Closing the connection
   $con -> close();
?>

Output

When the above code is executed, it will produce the following result -

SQL State Error: 42000

Example3

Following is another example of the PHP mysqli_sqlstate() function -

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   //Query to SELECT all the rows of the employee table
   mysqli_query($con, "SELECT * FROM employee");
   print("SQL State Error: ".mysqli_sqlstate($con)."\n");

   //Query to UPDATE the rows of 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 (Paul, 'Francis', 30, 'M', 13000, 106)");
   print("SQL State Error: ".mysqli_sqlstate($con)."\n");

   //Closing the connection
   mysqli_close($con);
?>

Output

When the above code is executed, it will produce the following result -

SQL State Error: 00000
SQL State Error: 42000
SQL State Error: 42S22

Example4

Try the following example below -

<?php
   $connection_mysql = mysqli_connect("localhost", "root", "password", "mydb");
   
   if (mysqli_connect_errno($connection_mysql)){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
   
   //Assume 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

When the above code is executed, it will produce the following result -

SQLSTATE error: 42S01


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP mysqli_ssl_set() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.