How can the mysql_error() function be used for debugging PHP scripts that interact with MySQL databases?

The mysql_error() function in PHP can be used to retrieve the last error message generated by a MySQL function. This can be helpful for debugging PHP scripts that interact with MySQL databases by providing information about what went wrong during database operations. By checking the error message returned by mysql_error(), developers can identify and address issues such as syntax errors, connection problems, or data manipulation errors.

// Example of using mysql_error() for debugging
$link = mysqli_connect("localhost", "username", "password", "database");

if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$query = "SELECT * FROM table";
$result = mysqli_query($link, $query);

if (!$result) {
    die('Query failed: ' . mysql_error());
}

// Continue with database operations