Getting Database Connection Failed Error in PHP Project

  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • Getting Database Connection Failed Error in PHP Project

    Hi everyone,

    I’m currently working on an AI and ML-based web application using PHP, and I've hit a roadblock. I’m encountering a “Database Connection Failed” error, and I’m not sure what’s causing it. Here are the details:

    Project Setup:

    Language: PHP
    Database: MySQL (version 8.4.0)
    Server: Apache
    PHP Version: 8.2​

    I'm getting this error-

    Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'@'hostname' (using password: YES) in /path/to/your/script.php on line X
    Database Connection Failed: Access denied for user 'username'@'hostname' (using password: YES)
    Here is the Connection Code:

    $servername = "hostname";
    $username = "username";
    $password = "password";
    $dbname = "database_name";
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Database Connection Failed: " . $conn->connect_error);
    echo "Connected successfully";

    I’ve verified that the username and password are correct by logging in directly to the database using these credentials.
    Ensured that the user has the necessary privileges to access the database.
    Confirmed that the hostname is correct (tried with 'localhost' and the IP address).
    Made sure the port number is correct and the database is running on the specified port.
    Checked for any firewall rules that might be blocking the connection.
    Verified that the MySQLi extension is enabled in my php.ini file.

    Despite these steps, I still get the same error. Could anyone suggest what might be going wrong or if there’s something else I should check? Any help or pointers would be greatly appreciated!

    Thanks in advance for your assistance.

    Best regards,
    Zuletzt geändert von admin; 02.07.2024, 08:52.