Mysql – Hostgator php code error while creating a new database table

MySQLPHP

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

Code is

{

<?php

$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.

Best Answer

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;
FLUSH PRIVILEGES;

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.