Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP MYSQL Query not fetching multiple WHERE Conditions

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What am I doing wrong here?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading