4

I have this line of code:

$mysqli = new mysqli("localhost", "user", "pass", "db");

I'm using XAMPP for the Apache and MySQL. If I use the line above it throws the following error:

Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'user'@'localhost' (using password: YES) in C:\xampp\htdocs\xo\php\connect.php on line 2
Failed to connect to MySQL: (1045) Access denied for user 'user'@'localhost' (using password: YES)
Warning: main(): Couldn't fetch mysqli in C:\xampp\htdocs\xo\php\connect.php on line 6

It's the same if I replace localhost with 127.0.0.1(I tried). But if I use my internal network IP(in this case 192.168.1.101) instead of localhost it connects successfully.

Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
Tenescu Andrei
  • 337
  • 1
  • 5
  • 15
  • 3
    You haven't provided access to `user@localhost` on your MySQL server, but you have done it for `user@192.168.1.101`. Just add the extra privileges –  Jul 15 '13 at 12:43
  • Wildcards for access apparently doesn't allow localhost or 127.0.0.1. It's either local or external, not both. Setting the access to localhost solved the issue. – ndm13 Mar 05 '15 at 20:56

6 Answers6

4

Try it this:

$mysqli = new mysqli("localhost", "user", "pass", "db", 3306);
                                                        port
lgabster
  • 695
  • 7
  • 17
1

I got the error:

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'dinesh'@'localhost' (using password: YES) in C:\xampp..............

I replaced localhost with my IP address and it finally worked in Windows 10.

SharpC
  • 6,974
  • 4
  • 45
  • 40
kcmaharshi
  • 23
  • 4
1

After upgrade from php5 to php7, the same problem stuck me for 2 days and finally I found out it is because at php.ini setup, you also need to define the socket location for mysqli. The original mysql got deprecate after php5.5 so after the upgrade, need to add mysqli.default_socket besides mysql.default_socket for its socket location.

Just need to make sure the following lines is at php.ini :

mysqli.default_socket = /path to/mysql.sock mysql.default_socket = /path to/mysql.sock

Jack Chen
  • 11
  • 2
0

you're having problems with your params

$mysqli = new mysqli($localhost, $user, $pass, $db);

$localhost, $user, $pass, $db (these params should be consistent)
Vítor Oliveira
  • 1,851
  • 18
  • 23
-1

try the following (answer based on official doc https://www.php.net/manual/en/mysqli.quickstart.connections.php ) :

$mysqli = new mysqli("127.0.0.1", "user", "pass", "db");

Adev
  • 1
-3

I think it should be:

$mysqli = new mysqli($localhost, $user, $pass, $db);
augustov
  • 1
  • 2