What are some common causes of a 'PDOException' with message 'invalid data source name' in PHP scripts?

A common cause of a 'PDOException' with message 'invalid data source name' in PHP scripts is when the database connection details are incorrect or not properly set up. To solve this issue, double-check the database connection settings in your PHP script to ensure they are accurate and match the database configuration.

<?php
$dsn = 'mysql:host=localhost;dbname=my_database';
$username = 'username';
$password = 'password';

try {
    $pdo = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
?>