How can error logs be utilized to troubleshoot PHP code errors related to database connectivity and query execution?

Error logs can be utilized to troubleshoot PHP code errors related to database connectivity and query execution by providing detailed information about the errors that occur. By checking the error logs, developers can identify the specific issues causing the errors, such as incorrect database credentials or syntax errors in SQL queries. This information can then be used to make necessary adjustments to the code to resolve the errors.

// Example of how to catch and log database connection errors in PHP

try {
    $pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    error_log('Connection failed: ' . $e->getMessage());
}