phpservices
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.
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.
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
FirstName as 'Andrew' OR LastName as 'Simon'
SELECT name FROM table WHERE name LIKE '%Andrew%' OR name LIKE '%Simon%'
--Raj
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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%';
B-( Stupid me, this $ was a good hint!
B-)
B-)
Yes this $ but not $this
;-)
~Ray
;-)
~Ray
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.
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.
SELECT name FROM table WHERE name LIKE '%search%'
--Raj