zaggs — 2009-12-08T06:29:12-05:00 — #1
I am performing a search on a MySQL database and need to match strings which begin with a string which is typed by the user.
For example, my user will enter the keywords "golf" in a search field and all matching rows will be returned. Example rows returned:
Only strings beginning with "golf" will be returned.
philiptoop — 2009-12-08T07:01:09-05:00 — #2
You need the Like operator and the wildcard character %.
where keyword like 'golf%'
zaggs — 2009-12-08T07:17:55-05:00 — #3
Ok, this works fine for the "begins with" search- Thanks! However, I also plan on having a "ends with" feature and this wildcard will not work for this as it's searching domain names. So the MySQL database contains data like the following:
If I use:
domain like '%".mysql_real_escape_string($_GET['keywords'])."'
This will not work as the domain ends in golf.com and it will not be matched. I guess I need to ignore any text after a dot, how can this be done?
philiptoop — 2009-12-08T08:20:30-05:00 — #4
The % means "any number of characters" but you can put specific characters in as well
and use the OR to look for different patterns.
keyword Like '%.golf%' or keyword Like 'golf%'
There is also a Regular expression operator which is more flexible still.
zaggs — 2009-12-08T09:27:26-05:00 — #5
Thanks for the suggestion, but unfortunately, it does not work. I think the error is in '%.golf%' as it will only return domains with the extension .golf right?
philiptoop — 2009-12-08T09:44:27-05:00 — #6
Sorry didn't get the full gish of your previous post. I was thinking of domain names like www.goftxxx.com. But rereading your previous post you talk about
The only way you are going to pick up both of these is to use '%golf%'. That is find golf anywhere in the domain name.
zaggs — 2009-12-08T09:46:24-05:00 — #7
Sorry, I made a typo above. The domains should have been:
How could I check the domains above?
philiptoop — 2009-12-08T09:50:32-05:00 — #8
keyword Like '%golf.%' or keyword Like 'golf%'
That is 'golf.' anywhere in the domain name or the domain name beginning with 'golf'