MySQL begins with string

Hi Guys!

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:

golfholiday
golftoday
golfers

etc…

Only strings beginning with “golf” will be returned.

Any ideas?

You need the Like operator and the wildcard character %.

where keyword like ‘golf%’

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:

todaysgolfer.com

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?

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.

Hi Phil,

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?

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

todaysgolfer.com
topgolf.com

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.

Sorry, I made a typo above. The domains should have been:

todaysgolf.com

How could I check the domains above?

keyword Like ‘%golf.%’ or keyword Like ‘golf%’

That is ‘golf.’ anywhere in the domain name or the domain name beginning with ‘golf’