We now have a youtube channel. Subscribe!

PHP | mysqli_error_list() Function

PHP mysqli_error_list() 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_error_list() Function.

The mysqli_error_list() function returns the list of errors which occurred during the last MYSQLi function call.

Syntax

Following below is the syntax to use this function -

mysqli_error_list($con)


Parameter Details

Sr.NoParameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.


Return Value

This function returns a list representing the errors (each of the error as an array) during the execution of the last function call.

PHP Version

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

Example1

The following example illustrates the usage of the PHP mysqli_error_list() function (in a procedural style) -

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

   mysqli_query($con, "CREATE TABLE Test(Name VARCHAR(10), AGE INT)");


   //Executing the query
   $query = "INSERT into Test values('Kennedy', 27),('Paul', 30),('Blessing Dauglas Queen Mikel', 25)";
   mysqli_query($con, $query);

   //Error
   $list = mysqli_error_list($con);
   print_r($list);

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

Output

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

Array
(
    [0] => Array
        (
            [errno] => 1406
            [sqlstate] => 22001
            [error] => Data too long for column 'Name' at row 3
        )

)

Example2

In an object oriented style the syntax of this PHP function is $con->error_list(); Following is the example of this function in an object oriented style $minus;

<?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 
   $list = $con->error_list;

   print_r($list);

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

Output

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

Array
(
    [0] => Array
        (
            [errno] => 1146
            [sqlstate] => 42S02
            [error] => Table 'mydb.wrong_table_name' doesn't exist
        )

)

Example3

Assume we have a table named employee with the following content -

mysql> select * from employee;
+------------+--------------+------+------+--------+
| FIRST_NAME | LAST_NAME    | AGE  | SEX  | INCOME |
+------------+--------------+------+------+--------+
| Kennedy    | Nkpara       |   27 | M    |  16000 |
| Justice    | Dauglas      |   27 | M    |  13300 |
| Justice    | Donald       |   28 | M    |  31000 |
| Stephanie  | Francis      |   23 | F    |   2256 |
| Bethel     | Igwela       |   27 | M    |  15000 |
+------------+--------------+------+------+--------+
5 rows in set (0.06 sec)

Following is another example of the built-in PHP mysqli_error_list() 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");
   $list = mysqli_error_list($con);
   print_r($list);

   //Query to UPDATE the rows of the employee table
   mysqli_query($con, "UPDATE employee set INCOME=INCOME+5000 where FIRST_NAME in (*)");
   $list = mysqli_error_list($con);
   print_r($list);

   //Query to INSERT a row into the employee table
   mysqli_query($con, "INSERT INTO employee VALUES (Precious, 'Amah', 25, 'F', 13000, 106)");
   $list = mysqli_error_list($con);
   print_r($list);

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

Output

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

Array
(
)
Array
(
    [0] => Array
        (
            [errno] => 1064
            [sqlstate] => 42000
            [error] => 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
        )

)
Array
(
    [0] => Array
        (
            [errno] => 1136
            [sqlstate] => 21S01
            [error] => Column count doesn't match value count at row 1
        )

)


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be studying about the PHP mysqli_field_count() 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.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain