简体   繁体   中英

MySQL remote connection with PHP

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'

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM