5

So I have a search feature on my website and this is what the code looks like.

$search_people = mysql_query("SELECT * FROM glnce_users 
                 WHERE f_name LIKE '%$search%' OR l_name LIKE '%$search%'");`

If i type in my search bar Chris it will bring up all of the Chris'

If I type in my search bar Olson it will bring up all of the Olson's

However if I type in Chris Olson it won't provide me results for Chris Olson even though there is a person with the First name of Chris and the Last name of olson.

What am I doing wrong?

1
  • 1
    You have two options: 1)Use two search bars, first-name and last-name and pass both variables to your current query string. 2)Use one search bar and assume a space separates the first from the last name and split. And as a bonus option you could change the query to combine first and last name and just use your single variable name.
    – styfle
    May 5, 2011 at 6:35

9 Answers 9

18

An exploded array seems bulky and error-prone. I would recommend concatenating your database columns in the query. That way if someone has a first name with a space in it, like "Sarah Jane Albertson", it won't look for a first name of "Sarah" and a last name of "Jane" and ignore the "Albertson" entirely.

This should do what you need:

$search_people = mysql_query("SELECT * FROM glnce_users 
    WHERE CONCAT(f_name, ' ', l_name) LIKE '%$search%' OR l_name LIKE '%$search%'");
1
  • Use COALESCE in case if the columns contain null value. Apr 13, 2017 at 6:45
3

Normal SQL,

$sql = "SELECT `id`, `first_name`, `last_name`, `mobile`, CONCAT(`first_name`, ' ',`last_name`) AS `full_name` FROM `users`
        WHERE `first_name` LIKE '%".$word."%'
        OR `last_name` LIKE '%".$word."%'
        OR CONCAT(`first_name`, ' ',`last_name`) LIKE '%".$word."%'
        OR `mobile` LIKE '%".$word."%';";

In Laravel,

$result = User::select('id', 'first_name', 'last_name', 'mobile', DB::raw('CONCAT(first_Name, " ", last_Name) AS name'))
        ->where('first_name', 'LIKE', '%'.$word.'%')
        ->orWhere('last_name', 'LIKE', '%'.$word.'%')
        ->orWhere(CONCAT(first_Name, " ", last_Name), 'LIKE', '%'.$word.'%')
        ->get();
2

you could use explode(). Here is some code which you could use:

<?php

$fullName = "Chris Olson";
$names_exploded = explode(" ", $fullName);   // will split " " (a space!)

counter_words = 0;

foreach($names_exploded as $each_name){
    $counter_words++;

/* check the word count */
if($counter_words == 1){
    $qPart .= " `f_name` LIKE '%$each_name%' OR `l_name` LIKE '%$each_name%'";
}else{
    $qPart .= " OR `f_name` LIKE '%$each_name%' OR `l_name` LIKE '%$each_name%'";
}

}

$q = mysql_query("ELECT * FROM `glnce_users` WHERE $qPart");
while($r = mysql_fetch_assoc($q)){
   // get your data here!
}

?>

Hope this helps. Check out this php search tut: YouTube

2
  • 1
    there is no need to put that type of query dude..Just use explode function and get what do you want..Thx. May 5, 2011 at 6:42
  • Sorry, but this is a horrible solution. Please simply do: WHERE CONCAT(f_name, ' ', l_name) LIKE '%$search%'
    – FooBar
    Feb 17, 2014 at 16:20
2
$searchArray = explode(" ", $search);

if (count($searchArray) > 1) {
$search_people = mysql_query("SELECT * FROM glnce_users WHERE f_name
    LIKE '%{$searchArray[0]}%' OR f_name LIKE '%{$searchArray[1]}%'
    OR l_name LIKE '%{$searchArray[0]}%' OR l_name LIKE '%{$searchArray[1]}%'");
} /* else do the original query */

You can switch the second OR with an AND if you want only Chris Olson be brought up, otherwise the Chris' and the Olson's will be brought up as well.

1

First of all use explode function to get that two words(Any number of words you searched) from space.and then just try to create query in FOR LOOP..

Then use that query. you will absolutely get whatever you wanted.

Thanks.

1

I have used this and its working fine

                   $q->where('first_name', 'like', "%$name%")
                   ->orWhereRaw("concat(first_name, ' ', last_name) like '%$name%' ")
                  ->orWhere('last_name', 'like', "$name");
0

use mysql full text search

http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html

0

I use like this in laravel:

     $q->where('first_name', 'like', '%'.$name.'%')
              ->orWhere('last_name', 'like', '%'.$name.'%')
->orWhere(DB::raw('CONCAT(first_name," ",last_name)'),'like','%'.$name.'%');
-2

Explode your string first to get each piece. Split on a space. Then compare ALL parts in the string against both first and last name.

Or, you can combine the first and last name columns on the select so that it would just query once against combined fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.