using search firstname and last name mixed php query

I have a SQL query command. The problem is when I type the whole name of a person it won't appear. For example if I search for "ermel", it will show ermel. However, when I search for "ermel lopez" it will fail to output the query. Here's my query:

      $query=mysql_query("select * from persons where firstname like '%$searchtext%' or lastname like   '%$searchtext%' order by date desc
      LIMIT $start,$per_page ");   

I tried CONCAT, which works, but won't work on the executable:

 SELECT *  
 FROM   persons 
 WHERE  CONCAT( firstname,  ' ', lastname ) LIKE 'kaitlyn' 
 OR     `LastName` LIKE  'pineda' 
 LIMIT 0 , 30

Upvotes: 1

Views: 1626

Answers (3)

McSenstrum
McSenstrum

Reputation: 127

Try reversing the way you check your strings like this:

"select * from persons 
where '$searchtext' like concat('%',firstname,'%') 
   or '$searchtext' like concat('%',lastname,'%')
 order by date desc
LIMIT $start,$per_page "

This should result in a match on your searchtext containing either the firstname or lastname.

Upvotes: 1

user2320325
user2320325

Reputation: 61

I think your Sql Query is wrong when you search Full name (ermel lopez) Then from your query, you search it in both columns. But whole text is not stored in a columns

Try this

$searchtext1="ermel";

$searchtext2= "lopez";

$query=mysql_query("select * from persons where firstname like '%$searchtext1%' or lastname like '%$searchtext2%' order by date desc LIMIT $start,$per_page ");

Upvotes: 0

basdwarf
basdwarf

Reputation: 422

a LIKE without wildcards is an exact match! you need Like 'kaitlyn%'

Upvotes: 0

Related Questions