Skip to content
PHP Logo

How can the use of mysql_error() function help in debugging PHP scripts that involve MySQL queries?

The mysql_error() function can help in debugging PHP scripts that involve MySQL queries by providing detailed error messages when a query fails. This can help developers identify the specific issue with the query and make necessary adjustments to fix it.

php
// Example of using mysql_error() function to debug MySQL queries in PHP
$conn = mysqli_connect("localhost", "username", "password", "database");

$query = "SELECT * FROM users WHERE id = 1";
$result = mysqli_query($conn, $query);

if (!$result) {
    echo "Error: " . mysqli_error($conn);
} else {
    // Process the query result
}

mysqli_close($conn);