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