1

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.