Home Backend Development PHP Problem How to query two tables and merge them using PHP

How to query two tables and merge them using PHP

Apr 11, 2023 am 09:14 AM

PHP is a programming language widely used in web development. When developing web applications, you generally need to connect to a database to obtain and store data, and querying data from multiple tables is a very common requirement. This article will explain how to query two tables and merge them using PHP.

First, we need to create two tables and insert some data into the tables. Suppose one table is named "orders" and the other is named "customers". The orders table includes order data, including order number, order date, order customer, etc. The customers table is customer data, including customer name, customer address, customer phone number, etc. To show how to query these two tables, we create the following table structure:

orders table

##12021-01-01122021-01-022##34customers table
order_id order_date customer_id
2021-01-03 1
2021-01-04 3

customer_id1234Next, we will use PHP to query these two tables and merge them into one table so that we can operate on the data. The following is the sample code:
customer_name customer_address customer_phone
Alice New York 123-456-7890
Bob Los Angeles 234-567-8901
Charlie Chicago 345-678-9012
David Houston 456-789-0123
// Connect to database
$host = "localhost";
$user = "username";
$password = "password";
$database = "database";
$connect = mysqli_connect($host, $user, $password, $database);

// Query orders table
$query = "SELECT * FROM orders";
$result = mysqli_query($connect, $query);

// Query customers table
$query = "SELECT * FROM customers";
$result2 = mysqli_query($connect, $query);

// Merge two tables
$merged_array = array();
while ($row = mysqli_fetch_assoc($result)) {
    $customer_id = $row['customer_id'];
    $customer_query = "SELECT * FROM customers WHERE customer_id = $customer_id";
    $customer_result = mysqli_query($connect, $customer_query);
    $customer_row = mysqli_fetch_assoc($customer_result);
    $merged_array[] = array_merge($row, $customer_row);
}

// Show merged table
echo "<table>";
echo "<tr><th>Order ID</th><th>Order Date</th><th>Customer Name</th><th>Customer Address</th><th>Customer Phone</th></tr>";
foreach ($merged_array as $row) {
    echo "<tr>";
    echo "<td>" . $row['order_id'] . "</td>";
    echo "<td>" . $row['order_date'] . "</td>";
    echo "<td>" . $row['customer_name'] . "</td>";
    echo "<td>" . $row['customer_address'] . "</td>";
    echo "<td>" . $row['customer_phone'] . "</td>";
    echo "</tr>";
}
echo "</table>";
Copy after login

In the above code, first connect to the database, then query the orders and customers tables, and store the results into the $result and $result2 variables. Next, we merge the two tables using a while loop and the array_merge function. In each loop, we first get the customer_id of a specific row from the orders table and then query the customers table to get the details of that customer. Finally, we merge the two arrays and add them to the $merged_array variable. Finally, we use a foreach loop to display the merged table in an HTML table.

Summary: In web application development, querying data from multiple tables is a common requirement. By using PHP we can easily query two tables and merge them. The code example above shows how to do this by connecting to the database, querying each table, and merging them.

The above is the detailed content of How to query two tables and merge them using PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24