简体   繁体   English

在 PHP 中使用 PDO 从多个表中进行搜索的更好方法是什么?

[英]Which is a better way to search from multiple tables using PDO in PHP?

I have an HTML form where there is a search field to type for a product name, and below are checkboxes, each for one shop where the product name is to be searched.我有一个 HTML 表单,其中有一个用于输入产品名称的搜索字段,下面是复选框,每个复选框对应一个要搜索产品名称的商店。 Users can search from multiple shops by checking the boxes.用户可以通过选中这些框从多个商店中进行搜索。 The form sends a GET request to server, where there is a database with separate tables for separate shops.该表单向服务器发送一个 GET 请求,其中有一个数据库,其中包含用于不同商店的单独表。

<form action="price.php" method="get">
    <input type="text" id="query" name="query" value="">
    <input type="checkbox" name="shop1" id="shop1">
    <input type="checkbox" name="shop2" id="shop2">
    <input type="submit" name="submit" value="submit">
</form>

So in server side I have written a PHP code that will search for the product name from those tables that correspond to the shops checked by the user.所以在服务器端,我编写了一个 PHP 代码,它将从与用户检查的商店对应的那些表中搜索产品名称。 Since I will be adding more and more shops in the future, which of the following PHP codes are better suited?既然我以后会加越来越多的店铺,那么下面的 PHP 代码哪个更适合?

VERSION 1版本 1

<?php
function search($pdo, $shop) {
if ( isset($_GET[$shop]) && ($_GET['query'] !== "") ) {
    switch ($shop) {
        case "shop1":
            $stmt = $pdo->prepare("SELECT * FROM `shop1` WHERE `name` LIKE :query");
            $stmt->execute(array(":query" => "%". $_GET['query'] . "%"));
            break;
        case "shop2":
            $stmt = $pdo->prepare("SELECT * FROM `shop2` WHERE `name` LIKE :query");
            $stmt->execute(array(":query" => "%". $_GET['query'] . "%"));
            break;
        ...
        ...
        ...
    }

    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    if ( count($rows) === 0 ) {
        $_SESSION[$shop] = 'nothing found from '. $shop;
        return array();
    } else {
        return $rows;
    }
    } else {
        return array();
    }
}

if ( ! isset($_GET['query']) ) {
    $_SESSION['success'] = "search for an item";
} else {
    $rowsShop1 = search($pdo, "shop1");
    $rowsShop2 = search($pdo, "shop2");
    ...
    ...
    ...
}
?>

VERSION 2版本 2

<?php
function search1($pdo, $shop, $sql) {
    $stmt = $pdo->prepare($sql);
    $stmt->execute(array(":query" => "%". $_GET['query'] . "%"));
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    if ( count($rows) === 0 ) {
        $_SESSION[$shop] = 'nothing found from '. $shop;
        return array();
    } else {
        return $rows;
    }
}

if ( ! isset($_GET['query']) ) {
    $_SESSION['success'] = "search for an item";
} else {
    if ( isset($_GET['shop1']) && ($_GET['query'] !== "") ) {
        $rowsShop1 = search1($pdo, "shop1", "SELECT * FROM `shop1` WHERE `name` LIKE :query");
    }
    if ( isset($_GET['shop2']) && ($_GET['query'] !== "") ) {
        $rowsShop2 = search1($pdo, "shop2", "SELECT * FROM `shop2` WHERE `name` LIKE :query");
    }
    ...
    ...
    ...
}
?>

Or is there a better way to do this?还是有更好的方法来做到这一点?

To go further than my comment, here is a fiddle so you can try the database model I described: https://www.db-fiddle.com/f/wPuc6P87MYuKKBYwztvK7X/0到 go 比我的评论更进一步,这里是一个小提琴,所以你可以试试我描述的数据库 model: httpsZ://www.db-fiddle/MYuKuKP8/KBP8

This can allow you to easily manage your shops and products through their own CRUD without having to work in several tables.这可以让您通过自己的 CRUD 轻松管理您的商店和产品,而无需在多个表中工作。

Here below the SQL code if the fiddle somehow doesn't work anymore.如果小提琴不知何故不再起作用,则在 SQL 代码下方。 (assuming MySQL / MariaDB) (假设 MySQL / MariaDB)

create table product
(
    id int auto_increment,
    name varchar(255) not null,
    constraint product_pk
        primary key (id)
);

create table shop
(
    id int auto_increment,
    name varchar(255) not null,
    constraint shop_pk
        primary key (id)
);

create table product_shop
(
  id int auto_increment,
  product_id int,
  shop_id int,
  quantity int not null default 0,
  constraint product_shop_pk
      primary key (id)
);

alter table product_shop
    add constraint product_shop_product_fk
        foreign key (product_id) references product (id);
alter table product_shop
    add constraint product_shop_shop_fk
        foreign key (shop_id) references shop (id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM