Forum Moderators: coopster

Message Too Old, No Replies

MySQL Selects

For a newbie...

         

TerryLM

7:43 pm on Oct 24, 2006 (gmt 0)

10+ Year Member



Ok so I just made my first sql database and PHP interface for it last week I'm getting pretty far with it however I'm having a problem with the select statement.

It's looking like this:

SELECT * FROM freehosted WHERE website='$website' AND type='FHG' AND category='$category' OR keywords LIKE '%$category%'

This is working good, however I ONLY want the website designated in the $website variable to display.

Right now it's displaying all over the map, if 1 variable matches it displays the record.

I want it to search like this:
Specific Website only
Type = FHG only
Category or keywords = Category (not only)

So basically the website and type have to be specifically as requested and the category just has to include it amongst other keywords/categories.

I really hope this can be done by select and not have to be broken down by some scripting or something. If so, please help me out here. I'm not NEW but I'm not professional by far.

Thanks a ton!

whoisgregg

8:36 pm on Oct 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello TerryLM! Welcome to WebmasterWorld! :)

I think what you want to do here is group some of your statements. There is an "order of operations" in SQL just like in algebra. In your case that OR is allowing anything to match because it's not clear to the SQL engine you intend it to be an OR that only relates to the category match. Try this code:

SELECT * FROM freehosted WHERE website='$website' AND type='FHG' AND ( category='$category' OR keywords LIKE '%$category%' )

TerryLM

3:40 pm on Oct 25, 2006 (gmt 0)

10+ Year Member



Thank you! That worked for sure. I figured it was something like that but I didn't know exactly what to use...

Thanks again!

-Terry

whoisgregg

6:32 pm on Oct 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Happy to help. :)