繁体   English   中英

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

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

我有一个 HTML 表单,其中有一个用于输入产品名称的搜索字段,下面是复选框,每个复选框对应一个要搜索产品名称的商店。 用户可以通过选中这些框从多个商店中进行搜索。 该表单向服务器发送一个 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>

所以在服务器端,我编写了一个 PHP 代码,它将从与用户检查的商店对应的那些表中搜索产品名称。 既然我以后会加越来越多的店铺,那么下面的 PHP 代码哪个更适合?

版本 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");
    ...
    ...
    ...
}
?>

版本 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");
    }
    ...
    ...
    ...
}
?>

还是有更好的方法来做到这一点?

到 go 比我的评论更进一步,这里是一个小提琴,所以你可以试试我描述的数据库 model: httpsZ://www.db-fiddle/MYuKuKP8/KBP8

这可以让您通过自己的 CRUD 轻松管理您的商店和产品,而无需在多个表中工作。

如果小提琴不知何故不再起作用,则在 SQL 代码下方。 (假设 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