How can proper error handling techniques be implemented in PHP to efficiently troubleshoot issues related to database connections?

When troubleshooting database connection issues in PHP, proper error handling techniques can be implemented by using try-catch blocks to catch exceptions that may occur during the connection process. By utilizing the PDO (PHP Data Objects) extension, specific error messages can be retrieved to pinpoint the root cause of the problem and facilitate efficient troubleshooting.

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