php mysql search feaure all keywords

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
craigy101
Forum Newbie
Posts: 6
Joined: Thu Jul 30, 2009 12:50 pm

php mysql search feaure all keywords

Post by craigy101 »

Hello I have a question regarding a PHP/MySQL search feature I am trying to implement --more leaning towards mysql if thats okay..

I'm implementing a search that will either search for ALL or ANY of the keywords the user specifies, I've created PHP code that generates the following MySQL query - (i've assumed the user has searched for two words 'facebook' and 'celebrity' and the columns that are being searched are 'keywords' and 'otherkeywords'.)

select * from articles where keywords like '%facebook%' || otherkeywords like '%facebook%' or keywords like '%celebrity%' || otherkeywords like '%celebrity%'

This works for the ANY option. When I tried creating one for the ALL keywords option, i first of all assumed it would be just a case of changing the OR to AND but this is not the case it would appear as it still produces the same results as the above query.

I've tried looking on various websites but can't find an answer to this. Does anybody know a query that will produce a match for all keywords (using the example above would be fine) thanks for any help

craig
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: php mysql search feaure all keywords

Post by McInfo »

You can use parentheses to group conditions in the WHERE clause.

Code: Select all

SELECT *
FROM `articles`
WHERE (`keywords` LIKE '%facebook%' OR `otherkeywords` LIKE '%facebook%')
AND (`keywords` LIKE '%celebrity%' OR `otherkeywords` LIKE '%celebrity%')
Unlike in PHP, in MySQL "OR" and "||" have the same operator precedence.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 4:21 pm, edited 1 time in total.
craigy101
Forum Newbie
Posts: 6
Joined: Thu Jul 30, 2009 12:50 pm

Re: php mysql search feaure all keywords

Post by craigy101 »

thanks - i expect this to work. i probably should have tried that i just didnt think! cheerz
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: php mysql search feaure all keywords

Post by aceconcepts »

For more insight into advanced searching capabilities takes a look at fulltext search
Post Reply