PHP Fatal error: Uncaught PDOException: could not find driver
When you encounter a PHP Fatal error: Uncaught PDOException: could not find driver, it is almost always a configuration issue rather than a problem with your application logic or database credentials. The fact that phpinfo() does not explicitly list the MySQL driver extension often leads developers to believe they have installed everything correctly, but this specific error message indicates that while the PDO core library is loaded, the specific adapter allowing communication between PHP and MySQL is missing or unlinked.
This separation between the core PDO module and the drivers is a common source of confusion. The base pdo extension provides the interface and abstraction layer, but it requires separate extensions for each database systemâsuch as pdo_mysql, pdo_pgsql, or pdo_sqlite. If you see this error in your stack trace, PHP has successfully initialized the PDO class, but when you attempt to instantiate a connection object with the mysql prefix, the runtime cannot load the underlying C extension required to talk to MySQL.
The Missing Extension
The most frequent cause of this issue is simply that the pdo_mysql extension is not loaded in your PHP environment. You can verify this by searching your phpinfo() output for "PDO Drivers." If you do not see an entry for mysql, or if the "Available drivers" section is empty, the driver is not active.
To resolve this on a standard Linux server using Apache or Nginx with PHP-FPM, you need to load the extension via configuration files. The location of these files varies by distribution:
- Ubuntu/Debian: Typically
/etc/php/{version}/apache2/mods-available/and/etc/php/{version}/cli/conf.d/. You would enable it usingsudo a2enmod php8.x-mysql(adjusting the version) or addingextension=pdo_mysql.soto the conf.d file. - CentOS/RHEL: Usually requires editing
/etc/my.cnf.d/client.cnfor creating a file in/etc/php.d/. You might addextension = pdo_mysql.sodirectly there.
Once you have ensured the extension file exists and is referenced in your configuration, you must restart your web server for changes to take effect. A simple systemctl restart apache2 or systemctl restart php-fpm will apply the new settings.
Environment-Specific Pitfalls
A subtle trap occurs when developing locally on macOS using Homebrew or Windows using WSL. These environments often separate system PHP from the version used by your web server. If you installed MySQL client libraries via a package manager but did not link them correctly to your PHP build, the PDO layer might fail to find the shared object files.
In these scenarios, simply installing the package isn't enough; you must ensure the libmysqlclient library is visible to the compiler and linker used during PHP's installation or extension loading. If you are using Composer to manage dependencies, ensure that any vendor-specific database drivers do not shadow the system-level PDO extensions unexpectedly, though this is rare for standard MySQL connections.
Code Considerations
Looking at your connection string:
$dbh = new PDO("mysql:host=127.0.0.1;dbname=radio;port=3306", $user, $pass);
The syntax here is correct for a standard MySQL connection. The error does not stem from the DSN (Data Source Name) format or the host/port specifications. It strictly points to the missing driver layer.
If you are deploying to a shared hosting environment, the situation differs slightly. You cannot always modify php.ini files directly. Instead, check with your host to see which PDO drivers are enabled by default. Many hosts pre-install pdo_mysql, but some minimalist configurations might only include pdo_sqlite. If you are forced to use a specific configuration file (like .user.ini), ensure the syntax is correct and that the extension path is valid for that specific user context.
Why phpinfo() Can Be Misleading
You mentioned checking phpinfo() and not seeing the error there. This happens because phpinfo() lists enabled extensions. If pdo_mysql is not loaded, it simply won't appear in the "PDO" section under "Loaded Extensions." It does not generate a warning unless you explicitly try to use the missing driver, which triggers the fatal exception at runtime. Therefore, the absence of mysql in your phpinfo() output alongside this error confirms that the extension is indeed disabled or missing from the load list.
For robust applications, especially if you are using frameworks like Laravel, it is sometimes helpful to define the default PDO driver class in config/database.php. However, this configuration only takes effect if the underlying driver is already loaded by PHP itself. You cannot configure PHP to use a driver that doesn't exist on the system; you must install and enable the extension first.
If you are building a large-scale application where database portability is critical, consider implementing a factory pattern or using an ORM like Eloquent (which works seamlessly with Laravel) to handle connection pooling and retry logic. While this doesn't fix the missing driver error directly, it provides a safety net against connection failures once the environment is correctly configured.