What tools or methods can be used to monitor the number of queries in PHP?

To monitor the number of queries in PHP, you can use tools like debugging extensions such as Xdebug or query profiling tools like MySQL's EXPLAIN statement. Another method is to manually log the queries using PHP's built-in functions like error_log or fwrite to a log file. By monitoring the number of queries, you can optimize your code and database interactions for better performance.

// Example code to log queries to a file
function logQuery($query) {
    $logFile = 'query_log.txt';
    $timestamp = date('Y-m-d H:i:s');
    $logMessage = "{$timestamp} - {$query}" . PHP_EOL;
    
    error_log($logMessage, 3, $logFile);
}

// Usage example
$query = "SELECT * FROM users";
logQuery($query);