Skip to content
PHP Logo

How can the mysql_error() function be used to identify errors in PHP MySQL queries?

The mysql_error() function can be used in PHP to retrieve the error message generated by the most recent MySQL operation. This can be helpful in identifying and troubleshooting errors in MySQL queries. To use mysql_error(), you can check the result of a MySQL query and then call mysql_error() to get the error message if the query fails.

php
// Perform a MySQL query
$result = mysql_query("SELECT * FROM non_existent_table");

// Check if the query was successful
if (!$result) {
    // Query failed, so get the error message
    $error_message = mysql_error();
    
    // Display the error message
    echo "Error: " . $error_message;
}