如何使用一个搜索栏搜索数据库中的多列 [英] How to search multiple columns in database with one searchbar

查看:48
本文介绍了如何使用一个搜索栏搜索数据库中的多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含几列的数据库表:

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 

这篇关于如何使用一个搜索栏搜索数据库中的多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆