第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 PHP 中使用 PDO 從多個表中搜索的更好方法是什么?

在 PHP 中使用 PDO 從多個表中搜索的更好方法是什么?

PHP
繁星coding 2023-07-08 20:14:48
我有一個 HTML 表單,其中有一個用于輸入產(chǎn)品名稱的搜索字段,下面是復(fù)選框,每個復(fù)選框?qū)?yīng)一個要搜索產(chǎn)品名稱的商店。用戶可以通過選中復(fù)選框從多個商店進(jìn)行搜索。該表單向服務(wù)器發(fā)送 GET 請求,其中有一個數(shù)據(jù)庫,其中包含針對不同商店的單獨表。<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>因此,在服務(wù)器端,我編寫了一段 PHP 代碼,它將從與用戶檢查的商店相對應(yīng)的表中搜索產(chǎn)品名稱。由于我將來會添加越來越多的商店,以下哪些 PHP 代碼更適合?版本1<?phpfunction 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<?phpfunction 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;    }}或者有更好的方法來做到這一點嗎?
查看完整描述

1 回答

?
幕布斯7119047

TA貢獻(xiàn)1794條經(jīng)驗 獲得超8個贊

如果小提琴不知何故不再工作,下面是 SQL 代碼。(假設(shè)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);


查看完整回答
反對 回復(fù) 2023-07-08
  • 1 回答
  • 0 關(guān)注
  • 131 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號