Skip to content
PHP Logo

What common error message indicates a connection issue between PHP and MySQL databases?

The common error message that indicates a connection issue between PHP and MySQL databases is "Warning: mysqli_connect(): (HY000/2002): Connection refused." This error typically occurs when the PHP script is unable to establish a connection with the MySQL database server, often due to incorrect credentials or server settings. To solve this issue, you should first double-check your database credentials in your PHP script to ensure they are correct. Additionally, make sure that the MySQL server is running and accessible from the host where your PHP script is running. Finally, verify that the MySQL server is configured to allow connections from the host where your PHP script is running.

php
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$database = "your_database";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>