What are some strategies for improving query debugging and error handling in PHP?

Issue: Debugging queries and handling errors in PHP can be challenging, but implementing proper error handling techniques can help identify and resolve issues more efficiently. Code snippet:

// Enable error reporting for PHP and database queries
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);

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

// Execute query and handle errors
$query = "SELECT * FROM users";
$result = $conn->query($query);

if (!$result) {
    die("Error executing query: " . $conn->error);
}

// Process query results
while ($row = $result->fetch_assoc()) {
    // Handle each row of data
}

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