Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

wildcard in where part of sql statement?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

wildcard in where part of sql statement?

Postby pugs421 » Fri Aug 01, 2003 5:50 am

Searching for home listings with multiple critera.

I'm trying to make a search feature that allows user to select the town, price range, and number of bedrooms in a home and return the results. Everything works except that I have to allow the user to choose 'any' from the drop down list for the bedrooms. In other words they shouldn't have to specify the bedrooms, they should be able to leave it at the default 'any' selection and get the results that match the other criterea, with ANY amount of bedrooms. This is my query:

"SELECT * FROM listings WHERE city = '$city_result' AND beds = '$beds_result' AND price BETWEEN '$min_price_result' AND '$max_price_result'"

In the 'beds' drop down menu the value for 'any' is * but that doesnt seem to work. Are you able to put a wildcard (*) in the WHERE part of the query? If not how would I make this work?
pugs421
New php-forum User
New php-forum User
 
Posts: 31
Joined: Sat Jun 28, 2003 9:09 pm

Postby pugs421 » Fri Aug 01, 2003 6:22 am

There must be a better way than the way I made this work. If a user selects 'any' from the drop down menu for number of bedrooms, the value assigned is zero. Then I have 2 seperate queries with a conditional statement deciding which one to use. I'm pretty new to this stuff and even though I got it to work I would really appreciate it if someone could show me a more efficient way.

New Code:

Code: Select all
if ($beds_result == 0)
{

$query_Rslistings = "SELECT * FROM listings WHERE city = '$city_result' AND beds >= '$beds_result' AND price BETWEEN '$min_price_result' AND '$max_price_result'";
$Rslistings = $connhomes->SelectLimit($query_Rslistings) or die($connhomes->ErrorMsg());

}
if ($beds_result > 0)
{

$query_Rslistings = "SELECT * FROM listings WHERE city = '$city_result' AND beds = '$beds_result' AND price BETWEEN '$min_price_result' AND '$max_price_result'";
$Rslistings = $connhomes->SelectLimit($query_Rslistings) or die($connhomes->ErrorMsg());
}
pugs421
New php-forum User
New php-forum User
 
Posts: 31
Joined: Sat Jun 28, 2003 9:09 pm

Postby pootergeist » Fri Aug 01, 2003 6:26 am

mysqls wildcard is %

$beds_result = ($beds_result == "*") ? '%' : $beds_result;

would just about do.

personally I'd build the where string prior to the query and append it to the select
pootergeist
New php-forum User
New php-forum User
 
Posts: 191
Joined: Wed Jan 29, 2003 7:11 am
Location: UK

Postby pugs421 » Fri Aug 01, 2003 8:43 am

Unfortunatly i have no idea what your talking about so since it works, i guess I'm ok with that.
pugs421
New php-forum User
New php-forum User
 
Posts: 31
Joined: Sat Jun 28, 2003 9:09 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.