How to Connect Remote MySQL Database using PHP

  1. Previous
  2. Next
  3. Recent

    1. Signs 'He' May be Cheating on You
    2. 7 Vows of Hindu Marriage - Know What 'Saat Vachans' Mean
    3. RuPay vs Visa vs Mastercard vs American Express- A Comparison
    4. What is RuPay? Everything You Need to Know About India's Domestic Payment System!
    5. Top 10 Online Portals to Find a High Paying Job in India
    6. Top Indians who Revolutionized the IT Sector in India
    7. 8 Initiatives by Narendra Modi that Could Change the Future of the Nation
    8. 8 Work-from-home Jobs for Housewives, Students, and Part timers
    9. Complete Company Registration Process in India - Explained
    10. Change Name in India in 5 Steps - Filing Affidavit, Newspaper Ad & Gazette Notification

For connecting to MySQL database hosted on the same server, we generally use ‘localhost’ as hostname. Apart from this, we need the username, password and name of the database we need to connect to.

Remote MySQL database means we have our database located at some distant location, not on the same place(host) where our website/app is. For connecting to such a database, we need to get its hostname, username and password.

After we have all this, here is how we make a connection to it using PHP,

1. Go to the (remote) server, and open database in phpMyAdmin.

connect remote mysql database using php-1

2. Copy the hostname appearing at the top.

connect remote mysql database using php-2

3. Get the database username and password. Its set at the time of database creation.

4. Once you have all this, make connection to this server from yours using the following PHP code,

$con = mysql_connect( 'remote-db-hostname', 'remote-db-username', 'remote-db-password' );
$selectdb = mysql_select_db( 'remote MySQL database name', $con );

For WordPress site

If you wish to show posts from a remote MySQL database in your WordPress site, just replace the hostname, username, password and database name with remote server’s in wp-config.php file inside WordPress folder.

connect remote mysql database using php-3

Once you do this, your WordPress site will start showing posts from that remotely located MySQL database.

blog comments powered by Disqus