How can error handling and debugging techniques in PHP be utilized to troubleshoot issues with SQL script execution?

When troubleshooting issues with SQL script execution in PHP, error handling and debugging techniques can be utilized to identify and resolve errors. One way to do this is by enabling error reporting and displaying error messages to understand what went wrong during SQL script execution.

// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Execute SQL query
$query = "SELECT * FROM users";
$result = mysqli_query($connection, $query);

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

// Close the connection
mysqli_close($connection);