Click to See Complete Forum and Search --> : how to do a "begins with" search?
Colm Osiris
05-24-2006, 05:25 AM
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.
$query="SELECT * FROM contacts WHERE (company='$search_company') or
(classification='$search_classification') ORDER BY company ASC";
Thanks.
chazzy
05-24-2006, 06:20 AM
This should be what you want:
$query="SELECT * FROM contacts WHERE company LIKE '".$search_company."%' or
classification like '".$search_classification."%' ORDER BY company ASC";
Colm Osiris
05-24-2006, 07:10 AM
Thanks, that seems to work, but it seems to conflict with other code I've got. Perhaps I'm trying to do too much.
chazzy
05-24-2006, 11:43 AM
what other code?
Colm Osiris
05-25-2006, 10:43 AM
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.
Colm Osiris
05-27-2006, 04:39 AM
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?
chazzy
05-27-2006, 07:51 AM
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.
Colm Osiris
06-02-2006, 02:37 AM
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:
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.