Problem:
Users encounter a PDOException with the message "could not find driver" when attempting to connect to a MySQL database using the PDO extension in PHP.
Code:
$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS)
Cause:
The error is most likely caused by the absence of the necessary PDO driver for MySQL (pdo_mysql).
Solution:
To resolve the issue, the pdo_mysql module needs to be installed and enabled in PHP.
Steps:
If not installed, use the PECL command to install it:
sudo pecl install pdo_mysql
Edit the php.ini file and uncomment the following line to enable it:
extension=pdo_mysql.so
Example:
sudo systemctl restart apache2
Note:
Make sure to replace sudo with your preferred root user command if necessary.
The above is the detailed content of Why Am I Getting a 'could not find driver' PDOException When Connecting to MySQL?. For more information, please follow other related articles on the PHP Chinese website!