我做了一个简单的PHP搜索功能,它可以搜索,product_name
但是我想添加另一个功能,以便它可以从product_name
OR中 搜索product_designer
。
搜索功能
function search($dbh, $word){
$query = "SELECT
product.product_name,
product.product_color,
product.year_released,
product.product_price,
product.product_image,
product.product_id,
product.product_desc,
product.product_designer,
type.name as type
FROM
product
JOIN type USING(typpe_id)
WHERE
product.product_name LIKE :word";
$stmt = $dbh->prepare($query);
$stmt->bindValue(':word', '%'.$word.'%', PDO::PARAM_STR);
$stmt->execute();
// fetch one product
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
函数调用
if(!empty($_GET['word'])) {
$products = search($dbh, $_GET['word']);
$title = 'Products like: ' . $_GET['word'];
}
只需使用OR条件:
$query = "SELECT
product.product_name,
product.product_color,
product.year_released,
product.product_price,
product.product_image,
product.product_id,
product.product_desc,
product.product_designer,
type.name as type
FROM
product
JOIN type USING(typpe_id)
WHERE
product.product_name LIKE :word
OR product.product_designer LIKE :word ";
$stmt = $dbh->prepare($query);
$stmt->bindValue(':word', '%'.$word.'%', PDO::PARAM_STR);
$stmt->execute();
// fetch one product
return $stmt->fetchAll(PDO::FETCH_ASSOC);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句