0

I'm new to MySQL and PHP and using 5.0 version of php. My requirement is to connect to a MySQL database which is located on another server (www.some-domain.com). My PHP files are located in my local system.

I have remote server database credentials. How do I configure these details into the PHP file ?

Here is my code so far:

mysql_connect("remote server ip", "root", "root123") or die (mysql_error ());
// Select database
mysql_select_db("resource") or die(mysql_error());
// SQL query
$strSQL = "SELECT * FROM resource WHERE resource_type=11";
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);

when i use above code getting below error:

Access denied for user 'root'@'%' to database 'resource'

vasudev
  • 410
  • 5
  • 13
  • 2
    Have you tried it and if yes, then what problem you are facing? – Code Lღver Apr 25 '13 at 06:01
  • 1
    see sidebar it having similar questions. – Yogesh Suthar Apr 25 '13 at 06:03
  • [`php_mysql`](http://php.net/manual/en/book.mysql.php) extension is [***deprecated***](http://php.net/manual/en/function.mysql-connect.php). I suggest to use [`php_mysqli`](http://php.net/manual/en/book.mysqli.php) or [`php_pdo`](http://php.net/manual/en/book.pdo.php) instead. – BlitZ Apr 25 '13 at 06:05
  • Try to connect using the server's IP address – Sid Apr 25 '13 at 06:15
  • And yes as CORRUPT mentioned php_mysql is deprecated, use mysqli or PDO. – Sid Apr 25 '13 at 06:21

2 Answers2

1

To connect to your mysql database in another server, you need to get the host, username and password of the database on that particular server. Like,

mysql_connect('server_host', 'server_db_username', 'server_db_password');
mysql_select_db('server_db_name');

Your code is correct. You only need to connect with the server credentials.

Sid
  • 854
  • 9
  • 23
1

There is a possibility, that remote connections are not allowed within MySQL Server access rules, as a common security measure. I suggest you to contact server support team for further details.

Also, I suggest to read this.

Community
  • 1
  • 1
BlitZ
  • 12,038
  • 3
  • 49
  • 68