Home > Net >  PHP MYSQL Query not fetching multiple WHERE Conditions
PHP MYSQL Query not fetching multiple WHERE Conditions

Time:12-24

I have a search feature that onkeydown gives out suggestions. 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.

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 i.e., id but not both.

What am I doing wrong here?

CodePudding user response:

I could be wrong, but WHERE customer_name will always evaluate to true and thus include all records. You need to specify what customer_name should be compared to.

Correct query:

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

As an important security tip, you should look into prepared statements instead of mixing data with your queries. Doing so leaves you vulnerable to SQL injection attacks.

  • Related