Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3356146
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:28:47+00:00 2026-05-18T02:28:47+00:00

Is there a way to make this query faster: $qur = mysql_query( SELECT id,

  • 0

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);

 } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:28:48+00:00Added an answer on May 18, 2026 at 2:28 am

    You can shorten the query to:

    $qur = mysql_query(
    "SELECT id, firstname, lastname, 
     (firstname = '$firstname' AND lastname = '$lastname') AS full FROM users 
     WHERE firstname LIKE '$firstname%' AND lastname LIKE '$lastname%' 
     ORDER BY (firstname = '$firstname' AND lastname='$lastname') DESC");
    

    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 $firstname and $lastname before inserting them in the query!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query similar to this FROM products AS p, .. LEFT JOIN
I'm far from being a mysql guru, so need to know if there's a
this question is from Codechef.com [if anyone is still solving this question dont look
I am trying to create a Class-Inheritance design for products. There is the base
I'm currently doing a summer job and I have to extend an existing program.
I am building a social app that has a chatView. Message is an entity.
I have the following tables: Products Categories Subcategories Brands with the following fields: Products:
I've got some inherited code that has a tendency to pass objects around as
(related to Finding the lowest unused unique id in a list and Getting unused
It might be a simple fix, but I can't for the life of me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.