Use mysqli extension technology to implement multiple data table queries
In the actual project development process, a project often requires multiple data tables to store information , and these tables are related to each other through primary keys, then this article will introduce how to implement queries on multiple data tables.
Then in our previous article "How to use mysqli extension technology to view server connection error reports", we introduced the method of mysqli extension technology to view server connection error reports. Today we will introduce to you the use of mysqli extension technology to implement queries between multiple tables!
Technical points
Using mysqli technology to implement multi-table queries, the key is how to connect multiple tables through primary keys. The following is the code for implementing multi-table query in this example:
$sql = "select * from student,score where student.id=score.id"; $result = mysqli_query($link, $sql);
To implement query between multiple tables, all table names should be listed after the from keyword, and the table names should be separated by commas. At the same time, the connection conditions between multiple tables should be specified in the where keyword. For example, student.id=score.id in this example indicates that the connection between the student table and the score table is through the id field of the student table and the sid field of the score table. connect.
Implementation process
##(1) Create a php file to implement the connection with the MySQL database connections between. The code is as follows:
$link = mysqli_connect("localhost", "root", "root"); $conn = mysqli_select_db($link, "php_cn"); $sql = "select * from student,score where student.id=score.id"; $result = mysqli_query($link, $sql); $res = mysqli_fetch_array($result, MYSQLI_ASSOC);
(2) Implement queries between multiple tables. If there are records that meet the conditions in the student table and grade table, these records will be displayed. , otherwise it will prompt that there is no relevant information. The code is as follows:
序列 |
名字 |
语文成绩 |
数学成绩 |
外语成绩 |
##Note:
The "sequence" and "name" fields in the above result diagram come from the student data table, while the other fields come from the score data table.We have introduced the mysqli expansion technology to implement multi-table query here. Friends can try it locally. In the next article, we will continue to explain the mysqli expansion technology. Please read "
Realize memory recycling through mysqli extension technology》! 【Related tutorial recommendations】
1. Relevant topic recommendations: "
php operating mysql database"2.【
MYSQL Online Free Video Tutorial】3. Related video course recommendations: "
Elementary MySQLi Extension Library Video Tutorial 》
The above is the detailed content of Use mysqli extension technology to implement multiple data table queries. For more information, please follow other related articles on the PHP Chinese website!