AllsWeb Blog
No Result
View All Result
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison
Support
Knowledgebase
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison
No Result
View All Result
AllsWeb White Logo
No Result
View All Result
Home PHP and MySQL

How to Connect to the Remote MySQL Database using PHP

Lakshika Mathur by Lakshika Mathur
January 1, 2020
Reading Time: 2 mins read
0
How to Connect to the Remote MySQL Database using PHP

There is a need for some web projects for accessing multiple databases on a different server. Therefore, you have to connect the remote database from another server. For some safety measures, disable the remote access to the MySQL database server. You have to enable remote MySQL access to connect the MySQL database from the different servers.

RELATED POSTS

What is Application Programming Interface (APIs)?

Like Dislike Rating System with jQuery, Ajax, and PHP

Star Rating System with jQuery, Ajax, PHP, and MySQL

Remote access will permit you to access the MySQL database from another server. This access is supportive when there is a need to connect multiple databases that are hosted on a different server. This sample script will explain to you how to connect to the remote MySQL database using PHP.

Let’s assume that the MySQL database is hosted on Server A, and you want to connect this database from Server B using PHP script. So, the remote database connection process would be the following.

  1. Log in to the cPanel account of the web server, where the MySQL database is hosted (Server A).
  2. Under the Databases section, click on Remote MySQL®.
  3. Enter the IP address of the host server (Server B) from where the database will be accessed. Click on Add Host.
  4. To connect with the MySQL database hosted in another server (Server A), use the following PHP code in Server A.
    <?php
    $dbServerName = "example.com";
    $dbUsername = "exdbuser";
    $dbPassword = "exdbpass";
    $dbName = "allsweb";
    
    // create connection
    $conn = new mysqli($dbServerName, $dbUsername, $dbPassword, $dbName);
    
    // check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";
    ?>
  5. Now you will able to reach through the database tables of the remote database server. 
<?php
/*
 * get data from remote database table
 */
$sql = "SELECT id, name FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results";
}
?>

The following example script will fetch the user’s data from the user’s table to the remote database.

Also, read our previous blog- How to Install LAMP (Linux, Apache, MySQL, PHP) on Ubuntu

Tags: MySQL DatabasePHP
ShareTweetSendShareSharePinScan
Lakshika Mathur

Lakshika Mathur

Related Posts

What is Application Programming Interface (APIs), Types, and Importance.
PHP and MySQL

What is Application Programming Interface (APIs)?

January 29, 2022
183
Like Dislike Rating System with jQuery, Ajax, and PHP
jQuery & AJAX

Like Dislike Rating System with jQuery, Ajax, and PHP

January 6, 2020
868
Star Rating System with jQuery, Ajax, PHP, and MySQL
jQuery & AJAX

Star Rating System with jQuery, Ajax, PHP, and MySQL

January 6, 2020
191
How to Force Download File in PHP
PHP and MySQL

How to Force Download File in PHP

January 2, 2020
89
How to Generate QR Code with PHP using Google Chart API
PHP and MySQL

How to Generate QR Code with PHP using Google Chart API

January 1, 2020
147
Send Email via SMTP Server in PHP using PHPMailer
PHP and MySQL

Send Email via SMTP Server in PHP using PHPMailer

December 31, 2019
56
Next Post
Form Validation using jQuery Validation Plugin

Form Validation using jQuery Validation Plugin

How to Force Download File in PHP

How to Force Download File in PHP

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Comparison (3)
  • HTML & CSS (9)
  • Interesting Facts (1)
  • JavaScript (27)
    • jQuery & AJAX (18)
  • PHP and MySQL (48)
  • Security (10)
  • SEO (2)
  • Trademark (2)
  • Tutorials (5)
  • Uncategorized (2)
  • Web Hosting (19)
    • VPS Server (5)
  • WordPress (8)

Recent Posts

  • Guidelines for Publishing Android and iOS apps on stores
  • Is the Trademark valuable to your Brand or domain?
  • Ideas For Ten Fantastic Online Business From Home
  • Home
  • Posts
  • Privacy Policy
  • Terms and Conditions

Built and Maintained With ♥ by AllsWeb Team

No Result
View All Result
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison

Built and Maintained With ♥ by AllsWeb Team

Go to mobile version