What resources or tools can be used for debugging SQL queries in PHP?

Debugging SQL queries in PHP can be challenging, especially when dealing with complex queries or database connections. One way to effectively debug SQL queries in PHP is by using tools like PHP's built-in error handling functions, such as mysqli_error() or PDO::errorInfo(). These functions can provide detailed error messages that help identify issues in SQL queries.

// Example of debugging SQL queries using mysqli_error()

// Create a database connection
$conn = mysqli_connect("localhost", "username", "password", "database");

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

// Run a SQL query
$sql = "SELECT * FROM users WHERE id = 1";
$result = mysqli_query($conn, $sql);

// Check if the query was successful
if (!$result) {
    die("Error executing query: " . mysqli_error($conn));
}

// Fetch and display results
while ($row = mysqli_fetch_assoc($result)) {
    echo "Name: " . $row['name'] . "<br>";
}

// Close the connection
mysqli_close($conn);