Fonction PHP mysqli_error ()

Définition et utilisation

le mysqli_error() function renvoie la description de l'erreur survenue lors du dernier appel de fonction MySQLi.

Syntaxe

mysqli_error($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_error () renvoie une valeur de chaîne représentant la description de l'erreur du dernier appel de fonction MySQLi. S'il n'y a pas d'erreur, cette fonction renvoie une chaîne vide.

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_error () (dans le style procédural) -

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

   //Query to retrieve all the rows of employee table
   mysqli_query($con, "SELECT * FORM employee");

   //Error
   $error = mysqli_error($con);
   print("Error Occurred: ".$error);

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

Cela produira le résultat suivant -

Error Occurred: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FORM employee' at line 1

Exemple

Dans le style orienté objet, la syntaxe de cette fonction est $ con -> error . 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 rows of employee table
   $con -> query("SELECT * FROM wrong_table_name");

   //Error 
   $error = $con ->error;
   print("Error Occurred: ".$error);

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

Cela produira le résultat suivant -

Error Occurred: Table 'mydb.wrong_table_name' doesn't exist

Exemple

Voici un autre exemple de la fonction mysqli_error () -

<?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("Errors in the SELECT query: ".mysqli_error($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("Errors in the UPDATE query: ".mysqli_error($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("Errors in the INSERT query: ".mysqli_error($con)."\n");
  
   //Closing the connection
   mysqli_close($con);
?>

Cela produira le résultat suivant -

Errors in the SELECT query:
Errors in the UPDATE query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*)' at line 1
Errors in the INSERT query: Unknown column 'Archana' in 'field list'

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();
   }
   
   if (!mysqli_query($connection_mysql,"INSERT INTO employee (FirstName) VALUES ('Jack')")){
      echo("Error description: " . mysqli_error($connection_mysql));
   }
   
   mysqli_close($connection_mysql);
?>

Cela produira le résultat suivant -

Error description: Unknown column 'FirstName' in 'field list'