Link to home
Start Free TrialLog in
Avatar of phpservices
phpservicesFlag for India

asked on

PHP-MySQL search Query to find names starts with letter entered in the text box

Hello.

I need to write search Query that returns the names that matches with the typed characters.. I have stored both first and last name in same column.  I need to search the typed characters with both First and Last Name (" All stored in Name field, separated by space )

Example:

name

Robert Mathew
Albert Mathew
Albert
Andrew Simon
Akhil Peter
Philp John

SELECT name FROM table WHERE name LIKE '$search%' OR name LIKE  '% $search%'

Can I write this ?  But this doesn't work.
Avatar of Rajkumar Gs
Rajkumar Gs
Flag of India image

Try this query

SELECT name FROM table WHERE name LIKE '%search%'

--Raj
If you want to search for 'Andrew Simon'.

FirstName as 'Andrew' OR LastName as 'Simon'

SELECT name FROM table WHERE name LIKE '%Andrew%' OR  name LIKE '%Simon%'

--Raj

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Bernard Savonet
Bernard Savonet
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In my example, I probably should have added some more PHP stuff - my intention of using the $c was to illustrate using a PHP variable in the construction of the MySQL query string.  More like this:
$c   = 'J'; // PROBABLY FROM A QUERY STRING LIKE /GET?c=J
$sql = "SELECT name FROM table WHERE name LIKE '$c%' OR name LIKE ' $c%';

Open in new window

B-( Stupid me, this $ was a good hint!
B-)
Yes this $ but not $this

;-)

~Ray
Avatar of davidurmann
davidurmann

Try doing it by
         SELECT column FROM table WHERE column RLIKE '^(search.*)|(.*[ ]+search.*)$'
Keep that space in square brackets as it is. You may find this one more compact and precise but I would be curious to know the performance on large data.