Searching with MySql, almost got it

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

Searching with MySql, almost got it

Post by invisibled »

Ok so i have a site where you can search business's that sign up. I have 7 different fields to search by. Right now this is my query that runs when you submit the form.

Code: Select all

 
// Build SQL Query  
$query = "SELECT * FROM ida_business WHERE
            industry LIKE '%$tindustry%' OR
            region LIKE '%$tregion%' OR
            range LIKE '%$trange%' OR
            flow LIKE '%$flow%' OR
            abs LIKE '%$abs%' OR
            sale LIKE '%$tsale%' OR
            price LIKE '%$tprice%' 
         ORDER BY region ASC";
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
 
I want it to only filter fields that the user selects from. And if they select more that one field it combines them and filters acordingly. I have the website online but dont want to post the link publicly, if you wish to see the site to get a better idea just say so and i will PM you the link.

This query isn't right but its the closest i could get it to being right. This is my first search script so any help will be great! If you need me to post more code just ask.

Thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Searching with MySql, almost got it

Post by Christopher »

Create an array of the field names from the form and loop thorough it to build the SQL.
(#10850)
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

Re: Searching with MySql, almost got it

Post by invisibled »

so like, If field is not empty, display the variable in the query?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Searching with MySql, almost got it

Post by Kieran Huggins »

exactly
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

Re: Searching with MySql, almost got it

Post by invisibled »

hmmmm i'm having alot of trouble with getting this going. could you show me what you mean? I get the concept, but I just can't quite figure out how to write it.
Post Reply