Unable to connect to mysql via php but works fine from command line

Go To StackoverFlow.com

1

I am just setting up a new VPS learning to setup my own web server and stuck at this pretty basic step.

PHP 5.3.3
MySQL 5.1.61
CentOS release 6.2 (Final)

I can connect to the server using mysql at the command line or remotely using Navicat without issues. I've put the host as '%' for testing as localhost wouldn't work either. I have tried with "localhost", IP, etc.

Connect failed: Access denied for user 'stats'@'localhost' (using password: YES)

Users:

+-----------+---------+
| Host      | User    |
+-----------+---------+
| %         | stats   |
| 127.0.0.1 | root    |
| localhost | root    |
+-----------+---------+

My code:

<?php
    $mysqli = new mysqli("localhost", "user", "db", "pw");
?>

Error when running the php script directly from the command line (No web server):

PHP Warning:  mysqli::mysqli(): (28000/1045): Access denied for user 'stats'@'localhost' (using password: YES) in /xxx/db.php on line 5
Connect failed: Access denied for user 'stats'@'localhost' (using password: YES)

The same is returned when running through apache.

Thanks in advance.

2012-04-05 18:01
by ruinernix
is your data base and your php code on the same server - Gerep 2012-04-05 18:03
Yes, that's why I'm using "localhost" to connect, thanks for helping clarifying that - ruinernix 2012-04-05 18:08
I asked that because it is a common mistake - Gerep 2012-04-05 18:10


2

This line:

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

Should be:

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

password comes before database name

2012-04-05 18:09
by Cal
Winner, wow. It proves that a second pair of eyes sometimes can fix the easiest of problems - ruinernix 2012-04-05 18:11
Oh I was going to.. I was constantly checking if I can click the checkmark! You answered too fast. : - ruinernix 2012-04-05 18:17


0

you're mixing mysql and mysqli

Edit

Check the permissions for the user you are trying to connect the db with.

2012-04-05 18:03
by Sujit Agarwal
The mysqli extension is an improved mysql extension for mysql 4.1+, I'm pretty sure this has nothing to do with the problem. I'm not getting an error with the extension, I'm getting a user access denied error - ruinernix 2012-04-05 18:06
then you should check for the permissions for the username u r usin - Sujit Agarwal 2012-04-05 18:08


0

Have you entered correct password? Check password

2012-04-05 18:07
by Gintas
The password is correct, as stated above I can login using the command line or remote access - ruinernix 2012-04-05 18:10
Ads