Skip to content
PHP Logo

What potential error messages can be generated when testing a MySQL connection in PHP?

When testing a MySQL connection in PHP, potential error messages that can be generated include "Access denied for user", "Can't connect to MySQL server", and "Unknown database". These errors typically indicate issues with the database credentials, server connection, or database name specified in the connection settings. To resolve these errors, ensure that the database credentials (username, password) are correct, the MySQL server is running and accessible, and the specified database exists.

php
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$database = "my_database";

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

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} else {
    echo "Connected successfully";
}

$conn->close();
?>