php: query mysql for every value array has in one statement

Joined
12 Feb 2006
Posts
17,889
Location
Surrey
struggling with this and Google doesn't seem to be given the answers i need.

I'm trying to create a simple Ajax search script which query's my database looking for matches like each word typed in the title and the description.

i have used preg_split to split each word over explode as i read that preg_split works better with multiple spaces. the results are stored in an array.

now i want to be able to query the database for each word in one statement but can't figure out how i'd do this without using a for loop. i was expecting to be able to use something like splitSearchWords[x]. so how do i do it?

thanks


also while got this thread open anyone got any tips for searching with php? trying to read stuff about it online but it's hard finding which is good advice and which is bad.
 
To answer your 2nd question, the sitepoint php architecture forum is fairly good.

For the 1st, since it's probably a "fuzzy" match you're looking for you could always do

Code:
SELECT id, title, content FROM table WHERE title LIKE '%word%' OR content LIKE '%word%' OR title LIKE '%word2% OR content LIKE '%word2%' etc.

since OR in SQL isn't exclusive OR (any of the OR clauses can be satisfied).

However, if what you are trying to do is implement a search engine for a site, maybe try looking instead to a dedicated search system like the PHP port of Lucene (Zend_Search_Lucene)? It scales better than using MySQL fulltext queries and you can implement a stemmer and stop word filter in the analyser frontend.
 
Last edited:
Back
Top Bottom