PHP中mysqli_connect连接被拒及mysqli_real_escape_string参数异常求助
Hey there, let's work through these errors together—they all stem from a single root issue, so fixing the first one will clear up the rest!
First, let's look at the critical error that's causing everything else:
警告:mysqli_connect(): (HY000/2002): 由于目标机器主动拒绝,无法建立连接。位于C:\wamp64\www\loginsystem\includes\dbh.inc.php第9行
This means your PHP script can't establish a connection to your MySQL server. The subsequent mysqli_real_escape_string warnings happen because mysqli_connect() returns false (a boolean) when it fails, and you're passing that boolean value to a function that expects a valid mysqli connection object.
1. Check if MySQL is Running (WAMP Specific)
Since you're using WAMP, start by confirming the MySQL service is active:
- Look at your WAMP tray icon—if MySQL's indicator is orange/red, right-click the WAMP icon → hover over MySQL → select Start Service. Wait for it to turn green.
- Alternatively, open Windows Services: Press
Win + R, typeservices.msc, find the service namedMySQL(might beMySQL80or similar depending on your version), and make sure its status says Running.
2. Verify Your Database Connection Parameters
Open dbh.inc.php and look at the mysqli_connect() call around line 9. Double-check these details match your WAMP setup:
// Example valid connection code $conn = mysqli_connect("localhost", "root", "your_mysql_password", "your_database_name");
Key checks:
- Host: WAMP MySQL uses
localhostor127.0.0.1(don't use an external IP here). - Username: Default is
rootunless you changed it during setup. - Password: Some WAMP versions set a default password, others leave it blank. Confirm what you used.
- Database Name: Make sure you've already created this database in phpMyAdmin (via WAMP's menu).
3. Add Connection Failure Debugging
Modify your dbh.inc.php to explicitly show connection errors—this will help you narrow down issues faster:
$conn = mysqli_connect("localhost", "root", "your_password", "your_db"); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
Now if the connection fails, you'll get a specific error message instead of just the generic "refused" warning.
4. Fix the mysqli_real_escape_string Errors
Once your connection is successful, $conn will be a valid mysqli object, so calling mysqli_real_escape_string($conn, $your_value) will work as expected. That said, prepared statements are a safer alternative to prevent SQL injection (better than relying on mysqli_real_escape_string):
// Example prepared statement for signup $stmt = mysqli_prepare($conn, "INSERT INTO users (username, email) VALUES (?, ?)"); mysqli_stmt_bind_param($stmt, "ss", $username, $email); // "ss" = two string values mysqli_stmt_execute($stmt);
If you've checked the service and connection details and still get the "refused" error, verify that MySQL is using the default port 3306 and that no other program (like XAMPP, another database, or a firewall) is blocking it.
内容的提问来源于stack exchange,提问作者adeel shahid

