How to implement PHP query for products under a specific category

WBOY
Release: 2024-03-09 17:50:01
Original
400 people have browsed it

How to implement PHP query for products under a specific category

Title: How to implement PHP query for products under specific categories

In the process of website development, it is often necessary to query products according to specific categories and display them to users. It is very common to implement this function through PHP. The specific implementation method and sample code will be introduced below.

First, we assume that there is a database table products to store all product information. The table structure is as follows:

CREATE TABLE products (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    category_id INT,
    price DECIMAL(10, 2)
);
Copy after login

Among them, the category_id field is To store the category ID to which the product belongs.

Next, we need to write PHP code to query products under a specific category. First, connect to the database:

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
Copy after login

Then, we need to get the category ID passed in by the user and query the products under this category:

$category_id = $_GET['category_id'];

$sql = "SELECT * FROM products WHERE category_id = $category_id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Price: " . $row["price"]. "<br>";
    }
} else {
    echo "0 results";
}
Copy after login

In the above code, we pass $_GET['category_id']Get the category ID passed in by the user, query the product information under the corresponding category in the database based on the ID, and display the results in list form.

Finally, don’t forget to close the database connection:

$conn->close();
Copy after login

Through the above operations, we can realize the function of querying product information based on specific categories. This method is simple and straightforward, suitable for small websites and small data volumes. It should be noted that the code does not handle the security verification of the parameters passed by the user. It is recommended to add parameter filtering and SQL injection prevention measures in actual applications.

The above is the implementation method of querying products under a specific category in PHP. I hope it can help you.

The above is the detailed content of How to implement PHP query for products under a specific category. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!