Home > Article > Backend Development > Detailed explanation of how to use PHP to implement product query function
The product search function is an essential function for modern e-commerce websites. It will enable users to easily search for the products they need, improve user shopping experience, and thereby increase website sales. This article will introduce how to use PHP to implement product query function.
1. Database design
First you need to create a database to store all product information. This database should contain at least some basic information such as product name, price, inventory, description, category, brand, etc. When designing this database, you should pay attention to the following matters:
In view of the characteristics of e-commerce platforms, attributes such as product names and prices change at any time. It is necessary to design a backend that allows administrators to update in real time to modify and update these data.
2. PHP connects to the database
First, a connection needs to be established so that PHP can interact with the MySQL database. During the connection process, you need to use the MySQL account and password, and you need to specify the connected database.
<?php $host = "localhost"; // 数据库地址 $user = "root"; // 数据库用户名 $password = ""; // 数据库密码 $database = "shop"; // 数据库名称 // 建立数据库连接 $conn = mysqli_connect($host, $user, $password, $database); // 检查连接是否成功 if (!$conn) { die("连接失败: " . mysqli_connect_error()); } ?>
3. Write SQL query statements
SQL is a standard language for managing databases. It is a very powerful tool that can be used through basic statements to implement a large number of functions. Here, we need to use SQL query statements to retrieve product information from the database.
The basic query statement is as follows:
SELECT * FROM products
This statement will select all columns and rows from the "products" table, that is, all product information.
If you need to specify some search conditions, such as searching for related products based on product names, you can use the WHERE keyword, as shown below:
SELECT * FROM products WHERE name LIKE '%iphone%'
This statement will start from "products" Select all products with "iphone" in their product name from the table.
4. PHP calls the query statement
In the PHP code, we need to combine the SQL statement with the PHP code and formulate the query conditions. Here is a basic code example:
<?php // 执行SQL查询 $search = mysqli_real_escape_string($conn, $_POST['search']); $query = "SELECT * FROM products WHERE name LIKE '%" . $search . "%'"; $result = mysqli_query($conn, $query); // 输出查询结果 if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { echo $row["name"] . " - " . $row["price"] . "<br/>"; } } else { echo "没有找到相关商品"; } ?>
In this example, we first get the search keywords from the user input via the POST method and escape the characters to avoid SQL injection attacks. Next, we execute the query statement and determine whether the query result is empty. If it is not empty, iterates through all matching results and outputs the name and price of each product.
5. Beautify the interface
Finally, we need to beautify the query results to make them easier for users to read and browse. You can put the query results in an HTML table and add CSS styles as follows:
<style> table, td, th { border: 1px solid black; border-collapse: collapse; } </style> <table> <tr> <th>商品名称</th> <th>价格</th> </tr> <?php if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { echo "<tr><td>" . $row["name"] . "</td><td>" . $row["price"] . "</td></tr>"; } } else { echo "<tr><td colspan='2'>没有找到相关商品</td></tr>"; } ?> </table>
In the above code, we create an HTML table and add borders using CSS styles. In the PHP code, we add each row of query results to the table and output a row of information when there are no results.
Conclusion
The above are the detailed steps to implement the product query function using PHP. Through this article, you should be able to understand and master how to connect to the database, write SQL statements, call query statements, and beautify the results. By learning this function, you should be able to further master some basic knowledge of PHP and lay a more solid programming foundation.
The above is the detailed content of Detailed explanation of how to use PHP to implement product query function. For more information, please follow other related articles on the PHP Chinese website!