-1

I have some problems with accessing my database.

The script worked before on my localhost. I imported it in another server and the other server is giving me an access denied message.

The message that is given is: Access denied for user 'root'@'10.4.1.163' (using password: YES)

The script I am using is:

<?php
    // Connect to database server
    mysql_connect("localhost", "root", "password") or die (mysql_error ());

    // Select database
    mysql_select_db("database") or die(mysql_error());

    // SQL query
    $strSQL = "SELECT * FROM users WHERE user_id='". $_SESSION['USER_ID'] ."'";

    // Execute the query (the recordset $rs contains the result)
    $rs = mysql_query($strSQL);

    // Loop the recordset $rs
    // Each row will be made into an array ($row) using mysql_fetch_array
    while($row = mysql_fetch_array($rs)) {

       // Write the value of the column FirstName (which is now in the array $row)
      echo $row['Name'] . " ";
      }

    // Close the database connection
    mysql_close();
?>

I also tried to change localhost to the IP address 10.4.1.163.

What is wrong with my script. I am sure that the password that I am using is right.

Does anybody know how I can fix this?

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
John
  • 904
  • 8
  • 22
  • 56
  • change localhost to 127.0.0.1 – num8er May 30 '16 at 22:33
  • 2
    You need to grant the user access to your database through your mysql client. This is not related to the code you use since it apparently works on another system. – Julie Pelletier May 30 '16 at 22:33
  • The user root have full rights. `Grant = yes` in DirectAdmin – John May 30 '16 at 22:35
  • 1
    Possible duplicate of ["Connect failed: Access denied for user 'root'@'localhost' (using password: YES)" from php function](http://stackoverflow.com/questions/6445917/connect-failed-access-denied-for-user-rootlocalhost-using-password-yes) – miken32 May 30 '16 at 22:42
  • FYI, you're using database functions that have been deprecated for more than five years, and that don't exist in the current version of PHP. Alternatives such as PDO or mysqli have been available for over a decade. – miken32 May 30 '16 at 22:43
  • This question has been asked and answered many times. A bit of google would take you straight to the most likely answer. – YvesLeBorg May 31 '16 at 00:25

1 Answers1

2

MySQL permissions are based on the address which they are connecting to as well as the user. So root@localhost and root@10.4.1.163 will have two separate set of permissions. Changing localhost to 127.0.0.1 as num8er mentioned will probably work if your code and the database are on the same server.

If you have terminal access to the box where your php is you could try connecting directly ruling out anything to do with php using this:

mysql -h 10.4.1.163 -u root -p[pass] database -e "SHOW TABLES"

Note there is no space between -p and the password. If successful this will get you a list of tables in database.

To grant access to other users or for another hostname/IP you will want to run something along the lines of this: (though you should really create a separate user with more restricted permissions based on your requirements).

GRANT ALL PRIVILEGES ON `database`.* TO 'root'@'10.4.1.163';

Check the docs on MySQL's GRANT here - http://dev.mysql.com/doc/refman/5.7/en/grant.html

On a side note - please, please, please don't just pump any old data into a query without at least using mysql_real_escape_string (http://php.net/manual/en/function.mysql-real-escape-string.php) on it before hand. You could also look into PDO (http://php.net/manual/en/book.pdo.php) which is generally preferred over the now deprecated mysql_ functions