-4

Error:

Warning: mysqli::mysqli() [mysqli.mysqli]: (28000/1045): Access denied for user 'Rohit'@'localhost' (using password: YES) in C:\xampp\htdocs\myprograms\db.php on line 8 Connection failed: Access denied for user 'Rohit'@'localhost' (using password: YES)

Code:

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

$sql = "SELECT cid, fname, lname FROM face";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

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

$conn->close();
?>  
halfer
  • 19,824
  • 17
  • 99
  • 186
Rohit Deshmukh
  • 364
  • 2
  • 5
  • 13
  • 2
    So, your connection failed because the user is denied access? – frz3993 Jul 07 '15 at 18:25
  • possible duplicate of ["Connect failed: Access denied for user 'root'@'localhost' (using password: YES)" from php function](http://stackoverflow.com/questions/6445917/connect-failed-access-denied-for-user-rootlocalhost-using-password-yes) – Machavity Jul 07 '15 at 18:27
  • Once you fix the connection your echo will still fail because the columns you are requesting are fname and lname, not `$row["firstname"] $row["lastname"]` so either alias them or change those indexes. – chris85 Jul 07 '15 at 18:27

2 Answers2

1

This is because you try to use incorrect username, password or host. Try to create new user from phpmyadmin page:

Open "Users" tab > select Global privileges checkboxes > type new User name, Host and Password and click to "Go" button.

After this try to set this new username, password and host in new mysqli() constructor.

Constantine
  • 482
  • 3
  • 10
0

The error message you are receiving means that the database connection failed. If you are using localhost on a Windows apache server it's common that the login to the database server is:

server=localhost
user=root
password= (blank)
databasename= your database name