phpmysqldatabaseconnectionhosted

How to fix the error: "Connection failed: Access denied for user 'username'@'servername' (using password: NO)"


I am trying to implement a login system on my website and therefore I need to connect a php file to a mySQL database. One problem, I keep getting this error:

Connection failed: Access denied for user 'username'@'servername' (using password: NO)

Does anyone know how to fix this error? Or can anyone tell me what I'm doing wrong?

I know this question has been asked like a million times already, but none of those answers have worked for me. They all are solutions for a local server and database, where mine is at a webhost so i think this works different.

Here's my code:

<?php

$servername = "servername";
$dBUsername = "db username";
$dBPassword = "db passwordd";
$dBName = "db name";
$dBPort = "db port";

$conn = new mysqli($servername, $dBUsername, $dPassword, $dBName, $dBPort);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

Solution

  • The variable name you are using in the password parameter slot is not the same as the one you defined above ($dBPassword vs $dPassword):

    $dBPassword = "db passwordd";
    // ...
    $conn = new mysqli($servername, $dBUsername, $dPassword, $dBName, $dBPort);
    

    In PHP the undefined variable "$dPassword" will be treated as a NULL which is likely why you're getting that "using password: NO" in the returned error.