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

  • Home
  • SEARCH
  • 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 7631409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:20:48+00:00 2026-05-31T06:20:48+00:00

I have a mysql/php query which searches through a database and returns matches based

  • 0

I have a mysql/php query which searches through a database and returns matches based on a search string that has been input by the user, it is sorted by title ascending, however we would really like it to be sorted by best match (similar to the way ElasticSearch ranks search results with a score).

Currently, when searching for “quake II” the result list looks like:

- Quake
- Quake 4
- Quake II
- Quake III Arena

Expected result list must be:

- Quake II (corresponding exactly to the search)
- Quake III Arena (containing entirely the search)
- Quake
- Quake 4

The current php/mysql query is:

$nameKeys = explode(" ", $gamenamethathasbeensearchedfor);
$query = "SELECT id FROM games WHERE GameTitle LIKE '%$nameKeys[0]%'";
for($i = 1; $i <= count($nameKeys); $i++)
{
    if($nameKeys[$i] != "" || $nameKeys[$i] != " " || $nameKeys[$i] != "  " || $nameKeys[$i] != "   ")
    {
        $query .= " AND GameTitle LIKE '%$nameKeys[$i]%'";
    }
}
  • 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-31T06:20:49+00:00Added an answer on May 31, 2026 at 6:20 am

    I would set up a FullText index on GameTitle, then you can take advantage of the power of MySQL’s full-text ordering and sorting without having to write some complex query. That is, if you are not using an InnoDB. You could do something like this as well,

    SELECT id, MATCH (GameTitle) AGAINST ('%$nameKeys[$i]%') AS SCORE FROM games WHERE MATCH (GameTitle) AGAINST ('%$nameKeys[$i]%') limit 10;
    

    This should get you pretty close without having to modify your indexes but I would recommend checking out MySQL Full-text functions

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

Sidebar

Related Questions

So I have this website that has a search feature which searches a table
I have the following mysql query which I am running with php like so.
I have a php script that runs a mysql query, then loops the result,
(Using MySQL and PHP) I have a search form that will allow my users
We have a query(mysql) which returns data in the following format of alphabets followed
Morning all! My MySQL query returns a PHP variable name which I'd like to
I have a PHP MySQL database which I will be storing all my information
I have a query like this (Mysql 5.X, PHP - formatted for legibility) $query
We have a PHP/MYSQL application that collects user input, including special characters like ø,ü,ñ,
I have a LAMP (Linux/Apache/MySQL/Php) application that I should release soon. Even if I've

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.