Home >Backend Development >PHP Problem >How to query two databases using PHP
PHP is an open source scripting language primarily used on the server side, often used for web development. It has the advantages of easy to learn, easy to use and high development efficiency, and has been widely used in website development. In actual development, you may encounter situations where you need to query multiple databases. This article will introduce how to use PHP to query two databases.
1. Connect to the database
In PHP, the mysqli extension and PDO extension are usually used to connect to the database. This article uses the mysqli extension for demonstration.
First create two databases, named db1 and db2 respectively, and create a student table and teacher table respectively in them. Both tables have fields such as id, name, age, etc. Then connect the two databases in the PHP code and query the data of the two tables. The code is as follows:
//Connect the db1 database
$mysqli1 = new mysqli(" localhost", "root", "password", "db1");
//Connect to db2 database
$mysqli2 = new mysqli("localhost", "root", "password", "db2 ");
//Query the student table in the db1 database
$sql1 = "SELECT * FROM student";
$result1 = $mysqli1->query($sql1);
while ($row1 = $result1->fetch_assoc()) {
//输出db1数据库中的student表的数据 echo "db1中student表的数据:" . $row1['id'] . " " . $row1['name'] . " " .$row1['age']. "<br/>";
}
//Query the teacher table in the db2 database
$sql2 = "SELECT * FROM teacher ";
$result2 = $mysqli2->query($sql2);
while ($row2 = $result2->fetch_assoc()) {
//输出db2数据库中的teacher表的数据 echo "db2中teacher表的数据:" . $row2['id'] . " " . $row2['name'] . " " .$row2['age']. "<br/>";
}
?> ;
In the above code, first use mysqli to try to connect to the two databases db1 and db2, and then query the data of one table each and output it on the page. A while loop is used to output data line by line.
2. Use the same connection to query two databases
After connecting to one database, you can use the connection object to query multiple databases. Just add the name of the database before the table names of other databases. Can. The sample code is as follows:
//Connect to db1 database
$mysqli = new mysqli("localhost", "root", "password", "db1");
//Query the student table in db1
$sql1 = "SELECT * FROM student";
$result1 = $mysqli->query($sql1);
while ($row1 = $result1->fetch_assoc()) {
//输出db1中的student表的数据 echo "db1中student表的数据:" . $row1['id'] . " " . $row1['name'] . " " .$row1['age']. "<br/>";
}
//Query the teacher table in db2
$sql2 = "SELECT * FROM db2.teacher";
$result2 = $mysqli->query($sql2);
while ($row2 = $result2->fetch_assoc()) {
//输出db2中的teacher表的数据 echo "db2中teacher表的数据:" . $row2['id'] . " " . $row2['name'] . " " .$row2['age']. "<br/>";
}
?>
In the above code, only one mysqli connection is used, but multiple databases can be queried. This can be achieved by using "." to connect the library name and table name.
3. Summary
This article introduces how to use PHP to query two databases and provides two methods. The first method is to use the mysqli extension to establish multiple connections for querying, and the second method is to use the same connection object to query multiple databases. In actual development, choose the appropriate method according to specific needs.
The above is the detailed content of How to query two databases using PHP. For more information, please follow other related articles on the PHP Chinese website!