Mysql 多个 LIKE 不起作用

发布于 2024-12-12 06:25:03 字数 866 浏览 3 评论 0原文

我有一张商店桌子。有些字段是名称、网站、描述等。

我有一个自动建议搜索栏。我想匹配输入并显示一些相关商店。现在,它可以完美地将用户输入与名称的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等你爱我 2024-12-19 06:25:04

likeand 因彼此相处不佳而臭名昭著。

使用括号来澄清,例如

SELECT linklabel,permalink 
FROM tblMerchants WHERE 
(name LIKE '%$input%') OR 
(website LIKE '%$input%')  OR 
(description LIKE '%$input%')

否则它可能会尝试猜测并做像

SELECT linklabel,permalink 
FROM tblMerchants WHERE 
name LIKE ('%$input%' OR 
website LIKE '%$input%')  OR 
(description LIKE '%$input%')

我不这样做的事情;如果上面是它会尝试的实际查询,但基本上它组合/链接它们的方式可能是“未定义” '并受解释。

likes and ands are notorius in not playing well with each others.

Use parentherses to clarify, e.g.

SELECT linklabel,permalink 
FROM tblMerchants WHERE 
(name LIKE '%$input%') OR 
(website LIKE '%$input%')  OR 
(description LIKE '%$input%')

Otherwise it can be trying to guess and do things like

SELECT linklabel,permalink 
FROM tblMerchants WHERE 
name LIKE ('%$input%' OR 
website LIKE '%$input%')  OR 
(description LIKE '%$input%')

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.

嘴硬脾气大 2024-12-19 06:25:04

代码很好(嗯,除了 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.

朕就是辣么酷 2024-12-19 06:25:04

试试这个:

"SELECT linklabel, permalink FROM tblMerchants WHERE
name like '%$input%'
UNION SELECT linklabel, permalink FROM tblMerchants WHERE
description like '%$input%'
UNION SELECT linklabel, permalink FROM tblMerchants WHERE
website like '%$input%'";

它应该有效。

Try this:

"SELECT linklabel, permalink FROM tblMerchants WHERE
name like '%$input%'
UNION SELECT linklabel, permalink FROM tblMerchants WHERE
description like '%$input%'
UNION SELECT linklabel, permalink FROM tblMerchants WHERE
website like '%$input%'";

It should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文