For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > January 2005 > Re: Simple(?) script problem for search by first character of a data









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Simple(?) script problem for search by first character of a data
Bob Stearns

2005-01-24, 8:56 pm

Shaun wrote:
> Hello ALT.PHP.SQL users :),
>
> I am having a small problem in this line which is a text box input
> search function:
>
> 1) $sql="SELECT * FROM pschamber WHERE Business_Name LIKE
> '%".$_POST['name']."%'";
>
> What it currently does is a google type search on the business names
> looking for rough matches. What I want to do is have it so when the
> SELECT input is say 'A' it only returns items from the field
> 'Business_Name' beginning with the selected input 'A'.
>
> I believe what I have to change from my line 1) is this section:
>
> 1a) LIKE '%".$_POST['name']."%'";
>
> Does anyone have any suggestions or weblinks to give me a rough ideas
> on how to have it match the first character.
>
> Thanks for your interest and help,
>
> Shaun

Just remove the first occurrence of the % (percent) which is SQL's wild
card match as many characters as possible. This change will match from
the left as many character as the user enters. Note that you are
allowing unedited user input into your query which can cause
catastrophic problems with your application.

LIKE '".$_POST['name']."%'"
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com