XAMPP/PHP报错:extension mysql is not compiled into PHP 求解决方案
Hey there, let's get this sorted out for you! The core issue here is that the old mysql.dll extension was completely removed in PHP 7.0+, and modern XAMPP versions don't include it at all—so trying to enable it won't work, even if you uncomment that line in php.ini. Here's what you need to do instead:
Step 1: Remove the outdated extension=mysql.dll line
First, head back to your php.ini file and delete (or re-comment) the line extension=mysql.dll. Since this extension no longer exists in your XAMPP setup, keeping this line will only trigger unnecessary errors.
Step 2: Verify pdo_mysql is properly loaded
You mentioned seeing pdo_mysql.dll in the ext/ directory—let's confirm PHP is actually utilizing it:
- Open
php.iniand look for the lineextension=pdo_mysql.dll(Windows) orextension=pdo_mysql(macOS/Linux). Make sure there's no leading semicolon (;) commenting it out. - Restart your Apache server from the XAMPP control panel.
- Create a quick
phpinfo.phpfile in your htdocs folder with this code:<?php phpinfo(); ?> - Access it in your browser (e.g.,
http://localhost/phpinfo.php) and search for "pdo_mysql". You should see a dedicated section confirming the extension is active.
Step 3: Update your PEAR DSN to use PDO-compatible syntax
Since you're using PEAR and a $dsn array, you need to adjust your DSN to work with PDO_MySQL instead of the deprecated mysql extension.
For example, if your old DSN looked like this:
$dsn = 'mysql://username:password@localhost/your_database';
Switch to the PDO-style DSN format:
$dsn = 'mysql:host=localhost;dbname=your_database;charset=utf8mb4';
If you're using PEAR's MDB2 library (a maintained alternative to the old PEAR DB), initialize the connection like this:
require_once 'MDB2.php'; $db = MDB2::connect($dsn, $options);
Key Reminder: The mysql extension is gone for good
PHP dropped support for the original mysql extension years ago due to security and obsolescence. All new (and updated) code should use either PDO_MySQL or mysqli instead. Since you already have pdo_mysql loaded, leaning into PDO is the smoothest path forward with your current setup.
内容的提问来源于stack exchange,提问作者Kevlwig

