Forum Moderators: coopster

Message Too Old, No Replies

Questions according to pattern matching with mysql select queries

or alternative solutions using php, maybe?

         

foy

2:11 pm on Mar 19, 2004 (gmt 0)

10+ Year Member



Hey there,

let's see...

I have a search function on my website and do get a string from a textfield($actor) which I compare with data in my mysql tables.

Let's say I have some rows in my table which consist of 1 field that have the following data:

1 Diaz, Cameron
2 Ford, Harrison
3 Connery, Sean
4 Barrymore, Drew
5 Jolie, Angelina
6 Bloom, Orlando

So, someone simply enters some characters into that textfield ($actor) and the data he entered is being compared with above rows.

Let's say he is looking for Harrison Ford and maybe just enters "Ford" as search query. So far so good.

My SELECT would look like this:

SELECT * FROM table WHERE actor = '$actor';

It won't give any search result yet the searched string does not exactly match the data in my table, so I'm altering the SELECT to this one:

SELECT * FROM table WHERE actor LIKE '%$actor%';

is that right?

NOW, to my problem:

If someone enters two characters, like only "OR" the search result looks like this:

2 Ford, Harrison
4 Barrymore, Drew
6 Bloom, Orlando

But I want Bloom, Orlando to be the FIRST result, since the "OR" is the first part of the surname. I mean, I don't want the script to look for any "or" or at least sort it a different way, so that more precise matches show on top of all results.

The SECOND problem I have, is that I want typing errors to be considered/allowed. So that e.g. someone looks for "ORLANDO BLOM" or "DREW BARRINGMORE" it might still give me the results I want instead of an ERROR or a 0-result.

Is there any way I can do this trick via PHP or is there even a way to do that via a MYSQL SELECT QUERY directly? I looked into regular expressions but I cannot seem to figure out how to do it right.

Thanks in advance!

Birdman

2:40 am on Mar 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, you can search the start of words by using LIKE '% $var%'. Adding a space before the first letter, However, the mispellings are going to be a much more complicated issue. Designing an efficient search engine is not easy. Sorry I didn't have a more promising answer for you ;(