How can MySQL errors be properly handled in PHP to troubleshoot database update issues?

When handling MySQL errors in PHP to troubleshoot database update issues, it is important to use error handling techniques such as try-catch blocks to capture and display any errors that occur during the update process. By using the mysqli_error() function, you can retrieve the specific error message generated by MySQL and take appropriate action based on the error. This helps in identifying the root cause of the issue and resolving it effectively.

<?php
// Establish a MySQL database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Perform database update
$sql = "UPDATE table_name SET column_name = 'new_value' WHERE condition";

try {
    if ($conn->query($sql) === TRUE) {
        echo "Record updated successfully";
    } else {
        throw new Exception("Error updating record: " . $conn->error);
    }
} catch (Exception $e) {
    echo $e->getMessage();
}

// Close database connection
$conn->close();
?>