0

I am trying to add filters to a DB search. I have a search that takes some text and tries to find items with that text in the title. I also have a price range filter. That code is below and works just fine

 $sql = "SELECT * FROM items where title LIKE '%". $title ."%' AND price > '". $price1 ."' AND price < '".$price2."' Limit 70";

Now I am trying to more and more filters. Is there a select from the above code's output? I don't want to just keep making a longer SELECT statement with tons of if statements. I'd prefer to take the output of the previous select and refine that with another select. Is this possible?

EDIT 1 Context:

Users are the ones entering the information. This is for searching the items on my site.

4
  • just curious what you are trying to achieve. It's completely unclear if you don't give more explanation. How would you like to refine? Commented Apr 13, 2016 at 3:51
  • Add you PHP that you are trying to do this in. Commented Apr 13, 2016 at 3:52
  • you should investigate using PDO and writing your queries with prepared statements and bound parameters to protect against LIKE '%" . $injection . " '% AND other ' " . bad things . " ' ... Commented Apr 13, 2016 at 4:24
  • @gavgri the user that I have php logged in as only has select permissions. Commented Apr 13, 2016 at 4:26

2 Answers 2

1

There's no other useful way than adding lots of different conditions to your WHERE cause, if you use plain SQL. It is possible to use several nasted SELECT statements in your query, but this makes your code neither any more readable nor faster.

A more elegant solution is the usage of query objects or another form of object-oriented query abstraction (e.g. ZendDB).

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

1 Comment

Thanks for the answer, I will accept it in the morning if no one proves you wrong
0

You can use some of the mysql string functions like INSTR(), MATCH which will make your life a little easy and also help the readability of the code. You can also use REGEXP and NOT REGEXP for pattern matching . The list of string functions are here.

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.