PHP 7.4无法加载php_mysqli.dll动态库,MySQL连接失败求助
Let’s work through this systematically—first, let’s rule out system/PHP configuration problems before we focus on Eclipse, since DLL loading failures almost always start there, not the IDE.
First: Verify the Problem Isn’t With PHP/System Configuration
Start by testing outside of Eclipse to isolate the issue. Open Command Prompt and run these checks:
1. Confirm PHP Thread Safety (TS) vs Non-Thread Safety (NTS) Match
PHP 7.4 on Windows comes in two flavors, and the php_mysqli.dll must match your PHP build:
- Run
php -vin cmd. Look for "Thread Safe" in the output (e.g.,PHP 7.4.33 (cli) (built: Aug 1 2023 11:50:48) ( ZTS Visual C++ 2017 x64 )means Thread Safe). - Check your
C:\Program Files\php\extdirectory: the DLL should be compiled for the same TS/NTS mode. If you downloaded the wrong PHP package, even the correct path won’t work—re-download the matching PHP version from the official site.
2. Install Required Visual C++ Redistributable
PHP 7.4 depends on the Visual C++ Redistributable for Visual Studio 2017/2019 (VC15/VC16). Missing or outdated versions will break DLL loading:
- Download the x64 or x86 version matching your PHP architecture (check
php -vfor x64/x86) and install it. Restart your system after installation.
3. Fix Directory Permissions
The C:\Program Files folder is protected by Windows UAC, which might block PHP from reading the DLL files:
- Copy your entire PHP directory to a non-system path like
C:\php(avoid spaces in the path if possible, though PHP 7.4 handles them better than older versions). - Update your
php.inito setextension_dir = "C:\php\ext", then runphp -magain to see if mysqli loads.
4. Validate PHP.ini Configuration
Double-check these settings in your active php.ini (run php --ini in cmd to confirm which php.ini PHP is using):
- Ensure
extension_dirpoints directly to your ext folder (use absolute paths, no trailing slashes). - Use the correct extension name: for PHP 7.4, it’s
extension=mysqli(notextension=php_mysqli.dll—the.dllsuffix is optional but often omitted in modern PHP configs). - Make sure there’s no semicolon (
;) before theextension=mysqliline (that comments it out).
If CLI Works But Eclipse Doesn’t: Check Eclipse Configuration
If php -m shows mysqli is loaded correctly in the command line, then the issue is with Eclipse’s setup:
1. Confirm Eclipse is Using the Correct PHP Executable
- Open Eclipse’s Window > Preferences > PHP > PHP Executables.
- Verify the path points to your working PHP
php.exe(the one you tested in cmd). - Check that the "PHP ini file" field points to the same
php.iniyou modified (not the defaultphp.ini-developmentorphp.ini-productionunless you renamed one tophp.ini).
2. Ensure Eclipse’s PHP Environment Matches CLI
- Sometimes Eclipse uses a different PHP environment profile. Go to Run > Run Configurations, select your PHP script, and check the "PHP Executable" dropdown matches the working one from your cmd tests.
3. Clear Eclipse’s Cache
IDE caches can hold outdated config data:
- Restart Eclipse with the
-cleanflag (open cmd, navigate to Eclipse’s installation folder, runeclipse.exe -clean). - Refresh your project: right-click the project in Eclipse’s Project Explorer > Refresh.
Final Note
If none of these steps work, try creating a simple test script (e.g., test.php with <?php phpinfo(); ?>) and run it via CLI (php test.php) to see if mysqli appears in the output. If it does, the problem is definitely Eclipse-specific—double-check all the IDE’s PHP settings again. If it still doesn’t load, you may need to re-download a fresh copy of PHP 7.4 (make sure to pick the correct TS/NTS and architecture) and start with a clean php.ini configuration.
内容的提问来源于stack exchange,提问作者StevieJ123

