www.webdeveloper.com
Results 1 to 8 of 8

Thread: how to do a "begins with" search?

  1. #1
    Join Date
    Jan 2006
    Posts
    64

    Question how to do a "begins with" search?

    Please can someone tell me how to make the following into a 'begins with' search, so that "inter" in the input field will bring up 'interest', 'internet', and 'interview'? It needs to be for any string length.

    I tried using LIKE, but it didn't like the percentage sign.

    Code:
    $query="SELECT * FROM contacts WHERE (company='$search_company') or 
    (classification='$search_classification') ORDER BY company ASC";
    Thanks.

  2. #2
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    This should be what you want:

    PHP Code:
    $query="SELECT * FROM contacts WHERE company LIKE '".$search_company."%' or 
    classification like '"
    .$search_classification."%' ORDER BY company ASC"

  3. #3
    Join Date
    Jan 2006
    Posts
    64
    Thanks, that seems to work, but it seems to conflict with other code I've got. Perhaps I'm trying to do too much.

  4. #4
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    what other code?

  5. #5
    Join Date
    Jan 2006
    Posts
    64
    See the thread 'passing data to a d/b via a hyperlink and without input fields'. I'm in the process of completely rewriting my code based on Gary's pseudo-code.

  6. #6
    Join Date
    Jan 2006
    Posts
    64
    hi Chazzy

    I've tried the code you suggested, but what happens is that all the records in the database are selected. Could this be to do with how the fields re defined?

  7. #7
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    could be lots of reasons.

    did you try validating your query? just go to the client tool and run your query with sample input, see what it returns.

  8. #8
    Join Date
    Jan 2006
    Posts
    64
    The reason it was selecting all the records was that I was searching on two fields, but I wasn't first checking whether both input fields were entered. So the query said:

    PHP Code:
    SELECT from contacts WHERE (company LIKE 'asda%') OR (classification LIKE '%'); 
    I now check the POST data before building the query, and it works fine.

    Thanks.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles