Is there a way to make this query faster:
$qur = mysql_query("
SELECT id, firstname, lastname,
(firstname = '$firstname' AND lastname = '$lastname') AS full FROM users
WHERE (firstname = '$firstname' AND lastname='$lastname')
OR (firstname LIKE '$firstname%' AND lastname LIKE '$lastname%')
OR firstname LIKE '$firstname%' OR lastname LIKE '$firstname%'
ORDER BY (firstname = '$firstname' AND lastname='$lastname') DESC");
What I need in this:
Check if its a full match.
Get firstname lastname
Being able to only enter lastname "Fox" and let it find the firstname (get all users with lastname Fox, and show their firstname too and display check the script at bottom.)
Being able to only enter the firstname "Megan" and let it find the lastname (^)
Being able to only enter Megan F, and let it show "Megan Fox"
Being able to only enter Me Fox, and let it show "Megan Fox"
This is what it does to me, working without no problem. Although maybe I think this would run slow when alot of users run it
I’m using it with this:
if (mysql_num_rows($qur) == 1) {
$get = mysql_fetch_array($qur);
if($get["full"] == 1){
echo $get["id"];
}else{
echo "Did you mean: ".$get["firstname"]." ".$get["lastname"]." ?";
}
}elseif(mysql_num_rows($qur) > 1){
while($get = mysql_fetch_array($qur)) {
$name[] = $get["firstname"]." ".$get["lastname"];
}
if(count($name) > 1) {
echo 'Who did you mean?<br>';
} else {
echo 'Did you mean: ';
}
echo implode('<br>', $name);
}
You can shorten the query to:
LIKE '...%'here covers all the cases. But apart from that, maybe you should look into MyISAM’s FULLTEXT search, that will probably yield better results.PS: I hope you cave escaped
$firstnameand$lastnamebefore inserting them in the query!