PHP | mysqli_error() Function

The mysqli_error() function is used to return the error in the most recent MySQL function call that failed. If there are multiple MySQL function calls, the error in the last statement is the one that is pointed out by the function.

Syntax:

mysqli_error("database_name")

Parameters: This function accepts single parameter as mentioned above and described below:

  • database_name: It is the database on which operations are being performed. It is a mandatory parameter.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$conn = mysqli_connect(
    "localhost", "root", "", "Persons"); 
        
// Check connection 
if (mysqli_connect_errno()) { 
    echo "Database connection failed."
  
// Check for error in query
if (!mysqli_query($link, "SET Age=1")) {
    printf("Error message: %s\n", mysqli_error($conn));
}    
  
mysqli_close($conn);
?>

chevron_right


Suppose the operation is being carried out on the table given below:



The output will be:

Error message: Unknown system variable 'Age'

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$conn = mysqli_connect(
    "localhost", "root", "", "Persons"); 
        
// Check connection 
if (mysqli_connect_errno()) { 
    echo "Database connection failed."
  
// Check for error in query
if (!mysqli_query($link, "SET Firstname='Arkadyuti'")) {
    printf("Error message: %s\n", mysqli_error());
}    
  
mysqli_close($conn);
?>

chevron_right


Output:

Error message: mysqli_error() expects exactly 1 parameter, 0 given

This example also demonstrates that mysqli_error() needs a database as a parameter.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.