How can one effectively debug SQL queries in PHP applications?

To effectively debug SQL queries in PHP applications, you can use functions like `mysqli_error()` to display any errors that occur during query execution. Additionally, you can enable error reporting in PHP to catch any syntax errors in your SQL queries. Another helpful tool is to log your SQL queries and their results to a file for further analysis.

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

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

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

// Execute a sample SQL query
$sql = "SELECT * FROM users";
$result = $mysqli->query($sql);

// Check for query errors
if (!$result) {
    die("Error executing query: " . $mysqli->error);
}

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

// Close the connection
$mysqli->close();