PHP mysqli: error_list() Function
mysqli_error_list() function / mysqli::$error_list
The mysqli_error_list() function / mysqli::$error_list returns a list of errors for the most recent function call, if any.
Syntax:
Object oriented style
array $mysqli->error_list;
Procedural style
array mysqli_error_list ( mysqli $link )
Parameter:
Name | Description | Required/Optional |
---|---|---|
link | A link identifier returned by mysqli_connect() or mysqli_init() | Required for procedural style only and Optional for Object oriented style |
Usage: Procedural style
mysqli_error_list(connection);
Parameter:
Name | Description | Required/Optional |
---|---|---|
connection | Specifies the MySQL connection to use. | Required |
Return value:
A list of errors, each as an associative array containing the errno, error, and sqlstate.
Version:PHP 5 >= 5.4.0, PHP 7
Example of object oriented style:
<?php
$mysqli = new mysqli("localhost", "user1", "datasoft123", "hr");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if (!$mysqli->query("SET a=1")) {
print_r($mysqli->error_list);
}
/* close connection */
$mysqli->close();
?>
Example of procedural style:
<?php
$link = mysqli_connect("localhost", "user1", "datasoft123", "hr");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if (!mysqli_query($link, "SET a=1")) {
print_r(mysqli_error_list($link));
}
/* close connection */
mysqli_close($link);
?>
Output:
Array ( [0] => Array ( [errno] => 1193 [sqlstate] => HY000 [error] => Unknown system variable 'a' ) )
Example:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform a query, check for error
if (!mysqli_query($con,"INSERT INTO employees (First_Name) VALUES ('David')"))
{
print_r(mysqli_error_list($con));
}
mysqli_close($con);
?>
Sample Output:
Parse error: parse error in C:\wamp\www\php\function-reference\mysqli.php on line 10
See also
PHP: Tips of the Day
To use the constant simply use its name:
Example:
if (EARTH_IS_FLAT) { print "Earth is flat"; } print APP_ENV_UPPERCASE;
Output:
or if you don't know the name of the constant in advance, use the constant function:
// this code is equivalent to the above code $const1 = "EARTH_IS_FLAT"; $const2 = "APP_ENV_UPPERCASE"; if (constant($const1)) { print "Earth is flat"; } print constant($const2);
Output:
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook