Access denied for user on MySQL database
Asked Answered
D

1

0

I have a single user for two databases on my website. First and older database DB_A is real database while newer DB_B is just for testing purposes.

My user, let's call it USER_X has all privileges (except 'DROP') on both of them and I'm sure that my username, password and DB name are all written correctly within PHP.

When connecting to DB_A and throwing if(!mysql_select_db($dbname)) echo mysql_error(); I get nothing, in other words, everything works fine.

When connectin to DB_B and throwing the same thing, I get Access denied for user 'USER_X'@'localhost' to database 'DB_B'.

Once again, I'm sure my credentials are correct and that I have all privileges on the DB_B database.

Anyone has an idea what's going on in here?

Thanks!

EDIT:

PHP v.5.2.17

MySQL v.5.0.96

Desultory answered 6/7, 2014 at 6:23 Comment(2)
"I'm sure my credentials are correct" well they are notTrigraph
Just read documentation carefullyStarvation
C
1

Use MySQLi - http://www.php.net/manual/en/book.mysqli.php - MySQL is deprecated.

It would be really useful to see your actual code!

Anyway, the following are from http://www.php.net/manual/en/mysqli.connect-error.php

I suggest trying them, if you still get an error, then check username, password and DB spelling are ok.

Object oriented style

$mysqli = @new mysqli('localhost', 'fake_user', 'my_password', 'my_db');

// Works as of PHP 5.2.9 and 5.3.0.
if ($mysqli->connect_error) {
     die('Connect Error: ' . $mysqli->connect_error);
}

Procedural style

 $link = @mysqli_connect('localhost', 'fake_user', 'my_password', 'my_db');

 if (!$link) {
     die('Connect Error: ' . mysqli_connect_error());
 }
Corrody answered 6/7, 2014 at 10:28 Comment(3)
So, here is the code with MySQLi $dbhost = 'localhost'; $dbuser = 'username'; $dbpass = 'password'; $dbname = 'user_databaseName'; $con = @mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); <br> if (mysqli_connect_errno()) {<br> ` printf("Connect failed: %s\n", mysqli_connect_error());` ` exit();` } mysqli_query($con, "SET NAMES 'utf8'");Desultory
Yeahm scram that indentation sh*t that doesn't work. Anyway, I get "Connect failed: Access denied for user 'username'@'localhost' to database 'user_databaseName'" as a result.Desultory
Comments are one line. I just tested the above on XAMPP and it worked flawlessly. What are you trying to do this on? Cpanel?Corrody

© 2022 - 2024 — McMap. All rights reserved.