MySQL: how do you query a price range?? min - max

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

MySQL: how do you query a price range?? min - max

Post by simonmlewis »

Code: Select all

$result = mysql_query ("SELECT * FROM dxvehicles WHERE make = '$make' AND title LIKE '%$model%' AND motability = '$motability' AND stockstatus = 'in stock' AND scrappage != 'scrappage' LIMIT $offset, $rowsPerPage") or die (mysql_error());
Here's the code.

I am also passing pricemin and pricemax through to this page. So basically I need to do something like this:

Code: Select all

SELECT * FROM dxvehicles WHERE price <= pricemax AND price >= pricemin
Is there such a query?

I have only ever done this in an IF statement after the query, but am sure there is a 'query' way of doing it.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: MySQL: how do you query a price range?? min - max

Post by Eran »

Did you try and run this query? it should work, so I'm not seeing the problem here.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: MySQL: how do you query a price range?? min - max

Post by jackpf »

You could use between. It shouldn't make any difference....but it'll make the query more readable.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: MySQL: how do you query a price range?? min - max

Post by simonmlewis »

I've made the <= work well.
Is 'between' a better way to code it? How do you write that?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: MySQL: how do you query a price range?? min - max

Post by Eran »

Post Reply