Access denied for user when connecting to godaddy database on windows hosting?

Khaldoun picture Khaldoun · Jul 18, 2015 · Viewed 11.7k times · Source

I am making simple mysqli connection to my database on Godaddy windows hosting, my code is as follows :

<?php 

        error_reporting(E_ALL & ~(E_STRICT|E_NOTICE));

            $host = 'localhost'; 
            $user = 'root_user_name'; 
            $password = 'root_user_pass'; 
            $db = 'my_database'; 
            $cxn = mysqli_connect($host,$user,$password) or 
                     die(mysqli_connect_error()); // <<<<<< line 9
            mysqli_select_db($cxn, $db ) or die(mysqli_connect_error()); 


        die('test');

I am sure of the granted username and password as well as the database name, all my credentials are proper, though I am getting this error:

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root_user_name'@'localhost' (using password: YES) in G:\path\to\file\index.php on line 9 Access denied for user 'root_user_name'@'localhost' (using password: YES)

I got this working on localhost with same database name and credentials using wamp server, I am not much into IIS server and windows hosting.. Any help will be appreciated.

Answer

KAD picture KAD · Jul 22, 2015

I once had this problem and simply resolved it by setting the real IP address of the database server in my host when connecting to the database. To get the IP address, log into your hosting account and access the databases, then click on managing the database you want, this will take you to phpMyAdmin. To the right there is a block with title Database Server, you can find the server IP address in the first list item. Use it in your $host rather than localhost :

 $host = '51.51.51.51'; // <<<< just a fake IP address for the example

I had a lot of harsh time to find out what was going wrong, I even contacted the support and got this reply :

Joshua - I think the permissions are the cause of the issue. Once you have them properly updated, the problem should go away.

They stated that the files permission could be the issue, I updated the file permissions but didn't resolve it.

Sometimes the solution is much closer than you can think of!!