Fonction PHP mysqli_sqlstate ()

Définition et utilisation

le mysqli_sqlstate() La fonction renvoie l'erreur SQLSTATE survenue lors du dernier appel de fonction MySQLi (opération MySQL).

Syntaxe

mysqli_sqlstate($con)

Paramètres

Sr. Non Paramètre et description
1

con(Mandatory)

Il s'agit d'un objet représentant une connexion à MySQL Server.

Valeurs de retour

La fonction PHP mysqli_sqlstate () renvoie une valeur de chaîne représentant l'erreur SQLSTATE survenue lors de la dernière opération MySQL. S'il n'y a pas d'erreur, cette fonction renvoie 00000 .

Version PHP

Cette fonction a été introduite pour la première fois dans la version 5 de PHP et fonctionne dans toutes les versions ultérieures.

Exemple

L'exemple suivant montre l'utilisation de la fonction mysqli_sqlstate () (dans un style procédural) -

<?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);
?>

Cela produira le résultat suivant -

SQL State Error: 42S02

Exemple

Dans le style orienté objet, la syntaxe de cette fonction est $ con -> sqlstate . Voici l'exemple de cette fonction dans le style orienté objet -

<?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();
?>

Cela produira le résultat suivant -

SQL State Error: 42000

Exemple

Voici un autre exemple de la fonction mysqli_sqlstate () -

<?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 (Archana, 'Mohonthy', 30, 'M', 13000, 106)");
   print("SQL State Error: ".mysqli_sqlstate($con)."\n");

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

Cela produira le résultat suivant -

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

Exemple

<?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);
?>

Cela produira le résultat suivant -

SQLSTATE error: 42S01