How can error handling be improved in PHP when executing delete queries to provide more detailed information on any issues that may arise?

When executing delete queries in PHP, it is essential to improve error handling to provide more detailed information on any issues that may arise. One way to achieve this is by utilizing the try-catch block to catch exceptions thrown during the query execution and then outputting the specific error message. This will help in identifying the root cause of the problem and taking appropriate action.

try {
    // Execute the delete query
    $query = "DELETE FROM table_name WHERE condition";
    $result = $conn->query($query);

    if (!$result) {
        throw new Exception($conn->error);
    }

    echo "Delete query executed successfully!";
} catch (Exception $e) {
    echo "Error executing delete query: " . $e->getMessage();
}