Rorschach
Rorschach

Reputation: 3812

select from a select statement in php & mySQL

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.

Upvotes: 0

Views: 69

Answers (2)

KP.
KP.

Reputation: 393

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.

Upvotes: 0

Andreas
Andreas

Reputation: 2837

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).

Upvotes: 1

Related Questions