如何使用一个搜索栏搜索数据库中的多列
问题描述:
我有一个包含几列的数据库表:
I have a database table with several columns:
Category Model Manufacturer
Roland Pianos | HP605 | Roland
Kawai Pianos | CL36 | Kawai
Roland Keyboards | BK9 | Roland
我希望能够同时搜索两列,所以当我输入 Roland HP605 搜索栏时,我希望结果显示 Roland HP605.
I want to be able to search both columns at the same time so when I put in the search bar Roland HP605 I want the result to show the Roland HP605.
目前我有以下几点:搜索框:
At the moment I've got the following: Search Box:
<input id="search" name="Search" type="text" placeholder="Search Products">
搜索代码:
if (isset($_GET['Search'])) {
$query_RS_Search ="SELECT * FROM products WHERE Category LIKE :search OR products.Manufacturer LIKE :search OR products.Model LIKE :search";
$RS_Search = $conn->prepare($query_RS_Search) or die(errorInfo());
$RS_Search->BindValue(':search', '%'.$_GET['Search'].'%');
$RS_Search->execute();
$row_RS_Search = $RS_Search->fetch();
如果你在搜索栏中输入Roland"就可以了,所有的 Roland 产品都有钢琴和键盘,但是如果我搜索Roland HP605",它将无法找到.
Which works fine if you put in the search bar "Roland" All Roland products come up both piano and keyboard, however if I search for "Roland HP605" it will not be able to find it.
欢迎任何帮助
答
试试这个
SELECT * FROM products WHERE CONCAT(Category, products.Manufacturer,products.Model) LIKE :search