PHP连接000webhost上的MySQL数据库报错,求确认正确主机名
Fixing MySQL Connection Failures on 000webhostapp
You’re right to suspect the hostname—localhost isn’t the correct value for connecting to your 000webhost MySQL database from your PHP script. Here’s how to resolve this:
Step 1: Get Your Correct Database Hostname
000webhost assigns a unique hostname to each database, which you can find in your account dashboard:
- Log into your 000webhost account and navigate to your site’s management panel.
- Look for the Manage Database section (usually under "Tools" or "Database" tab).
- In that section, you’ll see a field labeled Hostname—it typically looks like
sqlXXX.000webhost.com(where XXX is a unique number tied to your account).
Step 2: Update Your PHP Code
Replace the $servername value with the hostname you found. Here’s your modified code with debugging enabled to catch additional issues:
<?php // Enable detailed error reporting for debugging error_reporting(E_ALL); ini_set('display_errors', 1); // Replace these with your actual dashboard credentials $servername = "sqlXXX.000webhost.com"; // Your correct hostname $username = "your_database_username"; $password = "your_database_password"; $dbname = "your_database_name"; // Initialize database connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection status if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?> <html> <head> </head> <body> <h1>PHP connect to MySQL</h1> </body> </html>
Additional Troubleshooting Tips
- Double-check that your username, password, and database name exactly match what’s listed in your 000webhost dashboard (these values are case-sensitive!).
- Confirm your database user has permission to connect from the web server (000webhost usually configures this automatically, but you can verify it in the database management panel).
- Ensure no firewalls or security settings block outgoing connections on port 3306 (the default MySQL port).
内容的提问来源于stack exchange,提问作者A B




