Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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.

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

Thanks.
[507 byte] By [Colm Osiris] at [2007-11-11 8:45:25]
# 1 Re: how to do a "begins with" search?
http://www.google.com/search?q=php+mysql+like
Phil Weber at 2007-11-11 23:47:04 >
# 2 Re: how to do a "begins with" search?
LIKE is exactly what you need. You don't state what database engine you're using, but the syntax is generally WHERE company LIKE 'inter%'. How that plays with PHP I'm not sure, but it should be something along these lines:

$search_company = $search_company + "%";
@search_classification = $search_classification + "%";
$query="SELECT * FROM contacts WHERE (company LIKE '$search_company') or
(classification LIKE '$search_classification') ORDER BY company ASC";

Rune
Rune Bivrin at 2007-11-11 23:47:58 >
# 3 Re: how to do a "begins with" search?
thank you both very much
Colm Osiris at 2007-11-11 23:48:57 >