How to connect multiple MySQL databases on a single webpage ?
This article explains how to connect multiple MySQL databases into a single webpage. It is useful to access data from multiple databases.
There are two methods to connect multiple MySQL databases into a single webpage which are:
- Using MySQLi (Improved version of MySQL)
- Using PDO (PHP Data Objects)
Syntax:
- MySQLi Procedural syntax:
$link = mysqli_connect( “host_name”, “user_name”, “password”, “database_name” );
- MySQLi Object Oriented syntax:
$link = new mysqli( “host_name”, “user_name”, “password”, “database_name” );
- PDO (PHP Data Objects) syntax:
$pdo = new PDO( “mysql:host=host_name; dbname=database_name”, “user_name”, “password” );
Program: This program uses MySQLi to connect multiple databases on a single webpage.
<?php // PHP program to connect multiple MySQL datbase // into single webpage // Connection of first database // Database name => database1 // Default username of localhost => root // Default password of localhost is '' (none) $link1 = mysqli_connect("localhost", "root", "", "database1"); // Check for connection if($link1 == true) { echo "database1 Connected Successfully"; } else { die("ERROR: Could not connect " . mysqli_connect_error()); } echo "<br>"; // Connection of first database // Database name => database1 $link2 = mysqli_connect("localhost", "root", "", "database2"); // Check for connection if($link2 == true) { echo "database2 Connected Successfully"; } else { die("ERROR: Could not connect " . mysqli_connect_error()); } echo "<br><br>Display the list of all Databases:<br>"; // Connection of databases $link = mysqli_connect('localhost', 'root', ''); // Display the list of all database name $res = mysqli_query($link, "SHOW DATABASES"); while( $row = mysqli_fetch_assoc($res) ) { echo $row['Database'] . "<br>"; } ?> |
Output:

Recommended Posts:
- Nodejs – Connect Mysql with Node app
- MySQL | Common MySQL Queries
- How to redirect to another webpage in HTML?
- How to Add Google Charts on a Webpage?
- How to redirect to another webpage using JavaScript?
- Reader's View of a GeeksforGeeks webpage
- How To Add Google Translate Button On Your Webpage?
- Hide the cursor in a webpage using CSS and JavaScript
- Reading selected webpage content using Python Web Scraping
- How to check a webpage is loaded inside an iframe or into the browser window using JavaScript?
- Nodejs - Connect MongoDB with Node app using MongooseJS
- MySQL | BIN() Function
- PHP | MySQL WHERE Clause
- IFNULL in MySQL
- MySQL | MD5 Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



