How can developers effectively troubleshoot and debug issues related to database connections when using PDO in PHP?

To effectively troubleshoot and debug database connection issues when using PDO in PHP, developers can start by checking the connection parameters such as host, username, password, and database name. They can also use try-catch blocks to catch and handle any exceptions that may arise during the connection process. Additionally, enabling error reporting and logging can help identify any errors or warnings related to the database connection.

<?php
$host = 'localhost';
$dbname = 'my_database';
$username = 'root';
$password = '';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>