简体   繁体   English

PHP MYSQL 查询未获取多个 WHERE 条件

[英]PHP MYSQL Query not fetching multiple WHERE Conditions

I have a search feature that onkeydown gives out suggestions.我有一个搜索功能,onkeydown 会给出建议。 Currently, the query only fetches from one row of the table customer_name, and I'm trying to have the id row as well to be searched for the suggestions.目前,该查询仅从表 customer_name 的一行中获取,并且我正在尝试使用 id 行来搜索建议。

so my query syntax looks likes this:所以我的查询语法看起来像这样:

$query = "SELECT * FROM customers WHERE customer_name OR id like'%".$search."%' LIMIT 5";

But the above query will only fetches from the second table name ie, id but not both.但是上面的查询只会从第二个表名中获取,即 id 而不是两者。

What am I doing wrong here?我在这里做错了什么?

I could be wrong, but WHERE customer_name will always evaluate to true and thus include all records.我可能是错的,但WHERE customer_name将始终评估为true ,因此包括所有记录。 You need to specify what customer_name should be compared to.您需要指定应与哪个customer_name进行比较。

Correct query:正确查询:

SELECT * FROM customers 
    WHERE customer_name LIKE '%".$search."%'
    OR id LIKE '%".$search."%'
LIMIT 5;

As an important security tip, you should look intoprepared statements instead of mixing data with your queries.作为一个重要的安全提示,您应该查看准备好的语句,而不是将数据与查询混合。 Doing so leaves you vulnerable to SQL injection attacks.这样做会使您容易受到 SQL 注入攻击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM