mysqli_connect(): (HY000/2002): No connection could be made because the target machine actively refused it
When you encounter the mysqli_connect() error (HY000/2002): No connection could be made because the target machine actively refused it, the first instinct is often to check your firewall, restart services, or verify your credentials. While these are valid troubleshooting steps, they frequently miss the root cause in local development environments like XAMPP. The error message explicitly states that the target machine "actively refused" the connection. This distinction is critical: it does not mean the port is closed by a network filter; it means the operating system rejected the packet because no process was listening on that specific port.
In the scenario provided, the application attempts to connect to localhost:3360. However, standard MySQL installations typically default to port 3306. Unless you have explicitly configured your my.ini file to listen on a different port and verified that change took effect, the database server is almost certainly listening on 3306 while your PHP script requests 3360. The OS sees an incoming SYN packet for port 3360, finds no process bound to that port, and sends back a RST (reset) packet. This behavior triggers the "actively refused" error.
The code snippet reveals another significant syntax error that prevents the connection from establishing correctly, regardless of the port configuration:
$conn = mysqli_connect($dbhost, $dbuser, '', $db);
The mysqli_connect() function signature expects the arguments in this specific order: host, username, password, and optionally database. In your example, $dbhost is set to 'localhost:3360', but the password argument is an empty string (''). While an empty password might be valid for a root user in a fresh XAMPP install, the syntax of the connection string itself is the primary hurdle here. More importantly, you are attempting to select the database immediately upon connection.
The error handling logic in the script also contains a logical flaw that masks the true nature of the failure:
$sql = 'Connected Successfully';
$retvalue = mysqli_query($sql);
You are executing a string literal 'Connected Successfully' as if it were a SQL query. This is not valid SQL syntax and will almost certainly fail with an SQL error, but your if(! $retvalue) block catches that specific failure rather than the initial connection failure. Because the script dies inside the if(! $conn) check, you might not even see the query error unless the connection somehow succeeded (which it shouldn't given the port mismatch).
To resolve this, you must align your connection parameters with the actual configuration of your MySQL server. First, verify which port XAMPP is actually using. You can do this by checking the my.ini file located in your XAMPP installation directory for a line like port=3360. If that line does not exist or shows 3306, but you are trying to connect to 3360, the service is likely not running on that port.
Once you confirm the correct port, update your PHP configuration accordingly:
$dbhost = 'localhost'; // Remove the port number if using default socket connection or specify correct port
$dbport = 3306; // Or 3360 if explicitly configured in my.ini
// OR use a DSN-style string if supported by your driver version
// $dbhost = '127.0.0.1:3360';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $db);
if (!$conn) {
die('Could not connect: ' . mysqli_connect_error());
}
Notice the addition of mysqli_connect_error(). This function is essential for debugging. It returns a string describing the error (e.g., "Unknown MySQL server host" or "Can't connect to MySQL server") rather than just the generic driver error code. Without this, distinguishing between a network issue, a port mismatch, and a permission issue becomes a guessing game.
Furthermore, ensure your $dbpass variable is populated correctly. The source material mentions setting a password but seeing errors; if the XAMPP default configuration requires root with no password and your code expects one, or vice versa, the handshake will fail. However, the port mismatch remains the most probable cause of the "actively refused" message when XAMPP is confirmed to be running.
Finally, remember that mysqli_query() without a second argument executes a non-SELECT query expecting a result resource, but your string 'Connected Successfully' is not a valid query. If you intend to test the connection, simply echo the success message or run a simple SHOW DATABASES; query to verify access:
$sql = "SHOW DATABASES LIKE 'test_db13'";
$retvalue = mysqli_query($conn, $sql);
if (!$retvalue) {
// Handle error here
}
By correcting the port configuration and ensuring your connection arguments match the actual running service state, you can eliminate the active refusal error.