How can the use of die() after a MySQL query affect the execution of a PHP script?

Using die() after a MySQL query can halt the execution of a PHP script if the query fails, preventing any further code from running. To handle errors more gracefully, you can check the result of the query and display an appropriate error message instead of abruptly terminating the script.

<?php
// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Check connection
if (!$connection) {
    die("Connection failed: " . mysqli_connect_error());
}

// Perform a MySQL query
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);

// Check if the query was successful
if ($result) {
    // Process the query result
    while ($row = mysqli_fetch_assoc($result)) {
        // Do something with the data
    }
} else {
    echo "Error: " . mysqli_error($connection);
}

// Close the connection
mysqli_close($connection);
?>