Home >Backend Development >PHP Tutorial >PDOException 'could not find driver': How to Install the Missing PDO MySQL Driver?
PDOException "could not find driver": Troubleshooting and Solution
When encountering a PDOException indicating "could not find driver," the issue could lie in the absence of the necessary PDO driver for MySQL.
Identifying the Problem
The provided code snippet:
$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS)
attempts to connect to a MySQL database using the PDO driver. However, the exception suggests that the PDO driver is missing.
Solution: Installing PDO Driver
To resolve this issue, you need to install the pdo_mysql module. This module provides the PDO driver for MySQL and enables PHP to interact with MySQL databases.
To install the pdo_mysql module, run the following command:
sudo apt-get install php-pdo-mysql
Verifying Installation
Once the module is installed, you can verify its installation by viewing your PHP configuration information:
phpinfo();
Look for the following section in the output:
pdo_mysql PDO Driver for MySQL, client library version => 5.1.44
If this section is present, the pdo_mysql module is installed and you should be able to connect to your MySQL database using PDO.
The above is the detailed content of PDOException 'could not find driver': How to Install the Missing PDO MySQL Driver?. For more information, please follow other related articles on the PHP Chinese website!