Mysql 多个 LIKE 不起作用
我有一张商店桌子。有些字段是名称、网站、描述等。
我有一个自动建议搜索栏。我想匹配输入并显示一些相关商店。现在,它可以完美地将用户输入与名称的 LIKE 语句相匹配。
$input = $_GET["q"];
$data = array();
// query your DataBase here looking for a match to $input
include_once "scripts/connect_to_mysql.php";
$query = mysql_query("SELECT name,linklabel,permalink FROM tblMerchants WHERE name LIKE '%$input%'");
while ($row = mysql_fetch_assoc($query)) {
$json = array();
$json['name'] = ucwords($row['linklabel']);
$json['value'] = $row['permalink'];
$data[] = $json;
}
echo json_encode($data);
但是,我正在尝试将用户输入与表中的多个字段进行匹配。我正在尝试以下方法,但它不起作用:
SELECT linklabel,permalink
FROM tblMerchants
WHERE name LIKE '%$input%'
OR website LIKE '%$input%'
OR description LIKE '%$input%'
试图解决这个问题。任何意见表示赞赏!
抱歉,应该提到这一点。我所说的不起作用是指当使用多个 LIKE 语句时,它仍然只返回类似于“名称”的结果
I have a stores table. Some fields are name,website,description,etc.
I have an autosuggest search bar. I want to match input and display some related stores. Right now it works perfectly matching user input with a LIKE statement for name.
$input = $_GET["q"];
$data = array();
// query your DataBase here looking for a match to $input
include_once "scripts/connect_to_mysql.php";
$query = mysql_query("SELECT name,linklabel,permalink FROM tblMerchants WHERE name LIKE '%$input%'");
while ($row = mysql_fetch_assoc($query)) {
$json = array();
$json['name'] = ucwords($row['linklabel']);
$json['value'] = $row['permalink'];
$data[] = $json;
}
echo json_encode($data);
However, I'm trying to match user input with multiple fields in the table. I'm trying the following and it isn't working:
SELECT linklabel,permalink
FROM tblMerchants
WHERE name LIKE '%$input%'
OR website LIKE '%$input%'
OR description LIKE '%$input%'
Trying to figure this out. Any input is appreciated!
Sorry, should have mentioned this. By not working I mean that when using the multiple LIKE statement its still only returning results that are like 'name'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
like
和and
因彼此相处不佳而臭名昭著。使用括号来澄清,例如
否则它可能会尝试猜测并做像
我不这样做的事情;如果上面是它会尝试的实际查询,但基本上它组合/链接它们的方式可能是“未定义” '并受解释。
like
s andand
s are notorius in not playing well with each others.Use parentherses to clarify, e.g.
Otherwise it can be trying to guess and do things like
I don't don;t if the above is an actual query that it would try, but basically the way it would combine/chain them could be 'undefined' and subject to interpretation.
代码很好(嗯,除了 SQL 注入的危险以及它会快速增长的缓慢这一事实)。请显示包含您认为应该返回但在执行此查询时并未返回的行的示例数据。
The code is fine (well, except for the SQL injection danger and that fact that it will grow slow quickly). Please show sample data containing rows that you think should be returned but are not when you execute this query.
试试这个:
它应该有效。
Try this:
It should work.