0

every time I try to connect to database, I get this message database select error .Access denied for user '<username>'@'localhost' to database '<database>' . My site is hosted remotely . To connect to database , my connect.php is like :

    $host = 'localhost';
    $user = '<username>';
    $pass = '<password>';
    $db = '<database>';

    $conn = mysql_connect($host,$user,$pass) or die('Unable to connect to host ');

    @mysql_select_db($db,$conn) or die('database select error .'.mysql_error());

I have cross checked the username and password,its correct and I have given all the privileges to the user <username> .

Whats going wrong ?

Nitish
  • 2,695
  • 9
  • 53
  • 88
  • You say the site is hosted remotely; can we assume that this means that the database is on a different server than your php code? – jro Oct 24 '11 at 07:43
  • no its in same server. i mean to say its not in local pc,its hosted. sorry for my bad english – Nitish Oct 24 '11 at 07:46
  • Is the script being uploaded to your site first? If you are trying to run it from your local machine, then it probably won't work. Either upload the script and try it or enable remote connections and change localhost to your server address. – Tim Withers Oct 24 '11 at 07:48
  • All the scripts are uploaded to server. And if gave the IP address of server , it says `database select error .Access denied for user ''@'%' to database ''` – Nitish Oct 24 '11 at 08:00

2 Answers2

0

Most likely, the server on which the database is hosted it set to refuse connections from any request that doesn't originate from a white-listed source. This is particularly true if you're accessing a database on a shared-hosting plan, such as a Hostmonster, 1&1, etc. plan.

If it's not a shared host, you need to change "localhost" to be the IP and port number of the server+MySQL port,

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
  • Thanks Tieson,I think I got the answer . My site is hosted on shared hosting . That might causing the problem . So whats the solution? – Nitish Oct 24 '11 at 07:58
  • @Nitish Sadly, I can't give you an exact solution. It varies with the provider of your hosting services. There should be something in your control panel on the site to add your workstation as an authorized device; otherwise, contact the support desk. – Tieson T. Oct 24 '11 at 08:03
  • But I have a website in the same directory as this one,I am able to access the database for that site from the same device . I am total confused ! – Nitish Oct 24 '11 at 08:14
  • @Nitish Ok, looking at the comments above, are you getting this error from a site loaded on the hosted-server, or are you running the site locally? – Tieson T. Oct 24 '11 at 08:19
0

Try this

mysql_select_db($db,$conn) or die('database select error .'.mysql_error());
Rohan Patil
  • 2,328
  • 1
  • 16
  • 23