• 0

Search function in PHP/MySQL


Question

I'm considering writing a forum (of sorts, not the standard type like this), which all seems simple enough for someone with the PHP and MySQL experience that I have. However, there is one aspect I can't see a good solution to - the search function. I say this is for a forum but the theory (which is the problem, converting theory into PHP shouldn't be an issue) should stand for all searchable things like this, from the forum to a news posting script or whatever - I need to search through one or 2 fields in a table for some strings.

I want to have a box where the visitor enters the word or words they want to search for. Then there is a dropdown box or set of radio buttons where they select from the options "any word", "all words" or "exact phrase". There would be other options like by a certain member or within a time period, but these are no problem. They click on search and the script displays some details too all threads (or news articles or whatever) which contains any of the words, all of the words or the exact phrase.

I could implement a slow and painful method which would work by reading all entries in a table and individually searching the relevant fields, but that is both slow to implement and very slow to run. I can't see how to searching cut it down significantly, preferably cut down by the database. Obviously I can use WHERE and LIMIT sql parameters to cut out entries outside a set time or by a certain poster, but this leaves a lot of slow looping through returned fields for the PHP script to do.

Is there a way to get the database to search through the field(s) and narrow the results down?

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Interesting. So the % is like a * wildcard? What's the one character wildcard? Also, what if I want to search for a % - escape (ie use \%) it? So I can do any of these, depending on the option they choose:

WHERE field LIKE '%$exactPhrase%'

WHERE field LIKE '%$andWord1%' AND field LIKE '%$andWord2%'

WHERE field LIKE '%$orWord1%' OR field LIKE '%$orWord2%'

Correct? That's simple enough.

EDIT: Timdorr answered as I edited so missed my changes. I've found the one character wildcard is an _ - still not sure about escaping or not.

EDIT2: Yep, looks like escaping works if you want to search for %

Edited by Quboid
Link to comment
Share on other sites

  • 0

Wouldn't say Phorum is more clean to be honest. Webbox style (aka IPB's style) is a bit easier to follow. But that's just an opinion.

Also, might want to look at IPB 1.2 when that comes around. Matt used my idea for an abstracted search layer to support both FULLTEXT and older non-FULLTEXT mysql versions. Should be pretty cool :)

Link to comment
Share on other sites

  • 0
Wouldn't say Phorum is more clean to be honest. Webbox style (aka IPB's style) is a bit easier to follow. But that's just an opinion.

Maybe 'clean' wasn't the best word. I really meant that Phorum's code is more easier to read since it's more compact. IBF stretches among quite a number of pages for thousands of lines.

For actual usage, I find IBF to be excellent and use it on my own site but Phorum is a better learning tool IMO.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.