-1

I want to display data from a database from instead of showing the data in html it tells me "Connection failed: Access denied for user ''@'localhost' (using password: NO)"

I've tried searching the problem online and when other people had the same issue it was the case where they either had 'root'@'localhost' or 'user'@'localhost'. But in my case it doesn't even show any kind of username, it's blank.

<?php

$servername = "localhost";
$username = "john";
$password = "johndoe";
$dbname = "login";

// Create connection
$conn = new mysqli($localhost, $john, $johndoe, $login);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT firstname, lastname FROM cards";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"] . "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();


?>

1 Answers1

0

Where did you declare these variables?

$conn = new mysqli($localhost, $john, $johndoe, $login);

In your code, the real variable names are: $servername, $username, $password, $dbname and that's what you should use to retrieve their values, which are "localhost, "john", "johndoe" and "login".

Try:

$conn = new mysqli($servername, $username, $password, $dbname);

Gabriel Moretti
  • 676
  • 8
  • 23