All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Connect MySQL Database From Another Server In PHP

Last Updated : Mar 11, 2024

How To Connect MySQL Database From Another Server In PHP

In this article we will show you the solution of how to connect MySQL database from another server in PHP, we will see two different methods to connect the MySQL database to another server:

  • Mysqli_connect() method: this function is used to connect the MySql server. Syntax for Mysqli_connect() is mysqli_connect(host, username, password, dbname)
  • PDO class method: PHP Document Object of PDO is a framework that is used to access databases in PHP.

Step By Step Guide On How To Connect MySQL Database From Another Server In PHP :-

Let us the codes for two different methods to connect MySQL database from another server in php. At first, we created a server in localhost and created a database named ‘test’.

Method 1

In the example below, we used Mysqli_connect() method to connect the MySQL database.

<!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> how to connect mysql database from another server in php </title>
</head>
<body>
    <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1>
    <h2> how to connect mysql database from another server in php </h2>
</body>
</html>
<?php
$serverName = "localhost" ;
$userName = "root" ;
$password = "" ;
$dbName = "test" ;
//creating connection
$con = mysqli_connect($serverName, $userName, $password, $dbName) ;
if(mysqli_connect_errno ()) {
    echo " Failed to connect " ;
    exit () ;
}
echo "Connection Success!" ;
?>
  1. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here and also adding the inline CSS here.
  7. Now we close the HTML file with </html> tag.
  8. we write <?php tag to write PHP within it.
  9. Declaring variables for $serverName, $userName, $password, $dbName.
  10. Creating a connection with $con with mysqli_connect() function
  11. Using a if statement with mysqli_connect_errno(), using echo to display the error message, then exit()
  12. Then using echo to display a message that the server is successfully connected.
  13. ?> to close the php code.

Method 2

In the example below, we used the PDO class method to connect the MySQL database

<?php
$serverName = "localhost" ;
$userName = "root" ;
$password = "" ;
$dbName = "test" ;
//creating connection
try {
    $con = new PDO("mysql:host = $serverName; dbname = $dbName", $userName, $password) ;
    $con -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION) ;
    echo "Connection Success!" ;
}
catch (PDOException $e) {
    echo "Error in connection". $e -> getMessage () ;
}
  1. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here and also adding the inline CSS here.
  7. Now we close the HTML file with </html> tag.
  8. We write <?php tag to write PHP within it.
  9. Declaring variables for $serverName, $userName, $password, $dbName.
  10. Creating a connection with try using $con with new PDO() as the mysql host as $servername, dbName as $dbName, $userName and $password
  11. $con to setAtrributes(), using echo to display a message that the server is successfully connected.
  12. Now using catch() and echo to display a error message.
  13. ?> to close the php code.

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to connect MySQL database from another server in php.

I hope this article on how to connect MySQL database from another server in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪