PHP新手遇后台无法加载MySQL驱动问题:前台正常后台报错
Hey there, fellow PHP newbie! I totally get how frustrating it is when the frontend connects to the database just fine, but the backend throws this confusing driver error—let’s break this down step by step to fix it.
First, Compare Frontend & Backend Config Details
Since your frontend works, its database setup uses a supported driver. Here’s what to check:
- Dig into your working frontend config file and look for the database driver definition (it’ll likely be something like
define('DB_DRIVER', 'mysqli');). - Now open your backend config. If it’s set to
define('DB_DRIVER', 'mysql');, that’s the root issue! The oldmysqlextension was deprecated in PHP 5.5 and completely removed in PHP 7+. Swapmysqlwithmysqli(orpdo_mysqlif the script uses PDO) in your backend config.
Verify the mysqli Extension is Enabled
Even if you set the right driver, PHP needs the extension enabled to use it:
- Create a simple
phpinfo.phpfile in your project root with this code:<?php phpinfo(); ?> - Access this file via your browser (e.g.,
http://your-domain/phpinfo.php) and search for "mysqli". If you don’t see it listed, you’ll need to enable the extension:- Locate your
php.inifile (you can find its path in the phpinfo output). - Find the line
;extension=mysqliand remove the semicolon to uncomment it. - Restart your web server (Apache/Nginx) for the changes to take effect.
- Locate your
Check Backend Connection Code
Sometimes the config is correct, but the backend code still uses outdated mysql_* functions (like mysql_connect()). These functions are gone in modern PHP, so update the backend code to use mysqli_* functions or PDO:
- Example of a corrected mysqli connection:
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } - If using PDO, the connection string should look like:
$conn = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
Don’t forget to double-check all database parameters (host, username, password, database name) in your backend config—even a tiny typo can cause this error!
内容的提问来源于stack exchange,提问作者Regular Jo

