search
HomeBackend DevelopmentPHP ProblemHow to query two tables and merge them using PHP

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 "
"; echo ""; foreach ($merged_array as $row) {     echo "";     echo "";     echo "";     echo "";     echo "";     echo "";     echo ""; } echo "
Order ID Order Date Customer Name Customer Address Customer Phone
" . $row['order_id'] . "" . $row['order_date'] . "" . $row['customer_name'] . "" . $row['customer_address'] . "" . $row['customer_phone'] . "
";

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
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

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft