What are common reasons for the "Uncaught PDOException: invalid data source name" error in PHP?

The "Uncaught PDOException: invalid data source name" error in PHP typically occurs when the data source name (DSN) provided to the PDO constructor is incorrect or improperly formatted. This error can be solved by ensuring that the DSN is correctly specified with the appropriate database driver, host, database name, and other necessary parameters.

// Correcting the data source name (DSN) to fix the "Uncaught PDOException: invalid data source name" error
$dsn = "mysql:host=localhost;dbname=my_database";
$username = "username";
$password = "password";

try {
    $pdo = new PDO($dsn, $username, $password);
    // Additional PDO configurations or queries can be added here
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}