如何通过Cpanel修复PHP 7.4环境下因mysqli_connect()未定义导致的网站崩溃问题
Hey there, let's work through this mysqli_connect() undefined error you're hitting with your PHP 7.4 site in cPanel. I’ve dealt with this exact issue a handful of times, so here’s a step-by-step breakdown of what to try:
1. Verify the mysqli Extension is Enabled
First off, this error almost always means the mysqli extension isn’t loaded for your PHP version. Here’s how to check and enable it in cPanel:
- Log into your cPanel account, then look for Select PHP Version (sometimes labeled PHP Selector) under the Software section.
- Once you’re on that page, switch to the Extensions tab.
- Scroll through the list of extensions and find
mysqli—make sure its checkbox is checked. If it’s not, tick the box and click Save. Refresh your site afterward to see if the error is gone.
2. Check Your php.ini Configuration
If the extension is already enabled, the next thing to check is your php.ini file to ensure the extension isn’t commented out:
- From the Select PHP Version page, click Switch to PHP Options (or look for an option like Edit php.ini depending on your host’s cPanel setup).
- Use the search bar to look for
extension=mysqli. If you see a semicolon;at the start of that line, remove it (this uncomments the extension) and save your changes. - While you’re here, you can double-check that
mysqli.default_host,mysqli.default_user, and related settings are correct—but if your site was working before, this is less likely to be the issue.
3. Ensure Extension is Enabled for Your Target PHP Version
You mentioned trying to downgrade PHP but it didn’t help. Remember that each PHP version in cPanel has its own set of enabled extensions. If you switched to, say, PHP 7.3, you’ll need to:
- Go back to Select PHP Version, choose the downgraded PHP version from the dropdown at the top.
- Navigate to the Extensions tab again and confirm
mysqliis enabled for this version too. Save any changes and test your site.
4. Restart Your PHP/Apache Service
Sometimes changes to PHP extensions or php.ini don’t take effect until you restart the service. Here’s how:
- In cPanel, look for Restart Services (usually under Advanced or Server Configuration).
- Select either PHP-FPM (if your host uses it) or Apache and click the restart button. Wait a minute, then test your site again.
5. Confirm with phpinfo()
If you’re still stuck, create a quick test file to confirm if mysqli is loaded:
- Make a new file named
info.phpwith this content:<?php phpinfo(); ?> - Upload it to your site’s root directory, then visit
yourdomain.com/info.phpin your browser. - Use Ctrl+F to search for "mysqli"—if you don’t see any section dedicated to mysqli, the extension definitely isn’t loading, and you’ll need to revisit steps 1-3 or reach out to your host.
6. Reach Out to Your Hosting Provider
If none of the above works, there might be an issue with your host’s PHP environment (like mysqli not being compiled into their PHP build). Contact their support team, share the error message and what steps you’ve already tried, and they should be able to help resolve it on their end.
内容的提问来源于stack exchange,提问作者Ivan Rajkovača

