SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2007
    Location
    North Yorkshire, UK
    Posts
    483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need the Like operator and the wildcard character %.

    where keyword like 'golf%'

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PhilipToop View Post
    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
    topgolf.com

    If I use:

    PHP Code:
    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?

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2007
    Location
    North Yorkshire, UK
    Posts
    483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PhilipToop View Post
    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?

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2007
    Location
    North Yorkshire, UK
    Posts
    483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PhilipToop View Post
    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.
    Sorry, I made a typo above. The domains should have been:

    todaysgolf.com
    topgolf.co.uk

    How could I check the domains above?

  8. #8
    SitePoint Evangelist
    Join Date
    Jun 2007
    Location
    North Yorkshire, UK
    Posts
    483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    keyword Like '%golf.%' or keyword Like 'golf%'

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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •