1

When I try and connect to my database on XAMPP it gives me this error:

Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'user'@'localhost' (using password: YES)

I have created another user and am logged in to xampp with that. I use the Atom editor and am on macOS Mojave. This is my code -

<?php
 $dbhost = "localhost";
 $dbuser = "username";
 $dbpass = "password";
 $db = "dbname";


$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n".mysqli_connect_error());
echo "Connected Successfully";
return $conn;

mysqli_connect_error();

?>

I have written username and password for confidentiality. It is replaced by my real username and password in my code. The same applies to the 'user'@localhost in the error message

halfer
  • 19,824
  • 17
  • 99
  • 186
Sabad Modi
  • 45
  • 1
  • 6
  • I have updated the question. Please check – Sabad Modi Dec 01 '19 at 14:24
  • 1
    2 possible reasons: Wrong password or this user is not allowed to connect from localhost. `127.0.0.1` is something else. You need either `'user'@'%'` or `'user'@'localhost'` – colburton Dec 01 '19 at 14:28
  • Can it be because the server I am running my php code and xampp is the same? Both of them run on localhost:8080 – Sabad Modi Dec 01 '19 at 14:46

2 Answers2

0

I will use the following user as an example user@localhost, with a password 'password'.

In MySQL, a user is comprised of the following:

  1. A username - 'user'
  2. A password - 'password'
  3. A host address - 'localhost' (this is often an ip or a host name)

Connecting from the localhost machine with these credentials will work but if you connect from a different machine it will not connect.

I think you should debug this further, see if you get consistent results if you connect with those credentials from a different machine (via a cli client for example).

If you want to connect from multiple machines, you need to create multiple users that have different host addresses, oryou need a user that has a wildcard host %, for example user@%

liviu blidar
  • 351
  • 4
  • 16
-1

Go to your phpadmin page and check your username and password which exists in UserAccounts tab