Error is Error creating database: Access denied for user ''@'localhost' to database 'my_db'

Code is



$link=mysql_connect ("localhost", "cpuser_dbuser", "password") or die
 ('I cannot connect to the database because: ' . mysql_error());
 mysql_select_db ("cpuser_db");    

$sql = 'CREATE DATABASE my_db'; if (mysql_query($sql, $link)) {
    echo "Database my_db created successfully\n"; } else {
    echo 'Error creating database: ' . mysql_error() . "\n"; } ?> }

I have replaced all the credentials here.

Please help me solving this error.


This is not a PHP error, but a MySQL error. Basically, as the error message says you're either not using the correct username, password or the user privileges haven't been flushed since you added the user. That is, I'm assuming that you've removed the username from the error message, and not trying to log in with an empty parameter.

You need to check that the selected user has access to the database in question, and that the privileges has been flushed. Using the MySQL CLI client you can do this by the following commands:

USE mysql;
SELECT host,db,user,password FROM db;
SELECT host,user,password FROM user;

The latter should only be used after you've added, deleted or changed the permissions of any MySQL users in the above tables. If you don't have access, look at the MySQL manual for help on GRANT. Alternatively, search the web.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.