How can the debugging process be improved when encountering discrepancies between SQL query results in PHP code and direct database execution?

When encountering discrepancies between SQL query results in PHP code and direct database execution, one way to improve the debugging process is to print out the SQL query being executed in the PHP code. This can help identify any differences or issues with the query being sent to the database. Additionally, checking for any error messages or exceptions thrown by the database can provide insights into potential problems.

// Sample PHP code snippet to print out the SQL query being executed
$sql = "SELECT * FROM table_name WHERE condition = 'value'";
echo $sql; // Print out the SQL query being executed
$result = mysqli_query($connection, $sql);
if (!$result) {
    echo "Error: " . mysqli_error($connection);
}