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 6940081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:42:26+00:00 2026-05-27T12:42:26+00:00

I am trying to understand what is the best way to construct a query

  • 0

I am trying to understand what is the best way to construct a query string with php when there are multiple columns I want it to look for.

For example the database has id, name, email, date.

And I have a search input field on a page which when submitted, I want it to search based on the input field against the above mentioned columns. Best what is the best way/practice to do that?

I have the following so far but it seems like it is a “dumb” search.

"SELECT * FROM Table WHERE id LIKE '$search%' || name LIKE '$search%' || email LIKE '$search%' || date LIKE '$search%'";

Well this sort of works but I feel there has to be a better way and a more appropriate smarter method.

Thanks…

  • 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-27T12:42:27+00:00Added an answer on May 27, 2026 at 12:42 pm

    You cannot make mysql use index properly when you are querying multiple columns and not using AND (you are using OR)

    There is always need to optimize your query strategy, always people think the data is small,
    why bother to spend effort to optimize … but the truth is you don’t know when your data will grow

    For your query, if you want to stick to LIKE,
    then you need to build 4 indexes on the 4 columns,

    alter table Table add index on(id);
    alter table Table add index on(name);
    alter table Table add index on(email);
    alter table Table add index on(date);
    

    And change the query to :-

    SELECT * FROM Table WHERE id LIKE '$search%'
    union distinct 
    SELECT * FROM Table where name LIKE '$search%'
    union distinct 
    SELECT * FROM Table where email LIKE '$search%'
    union distinct 
    SELECT * FROM Table where date LIKE '$search%'
    

    What is Union ?

    Off-topic issue :-

    • you did not escape for user input, it could lead to SQL injection
    • is meaningless to search on ID when the input could be anything
    • same go for date
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decide on the best strategy for accessing the database. I understand
I'm trying to understand the best way to create line of business applications in
I am trying to understand conceptually the best way to deliver real streaming audio
so the best way for you to understand what I am trying to achieve
I'm trying to understand the best way to use Spring (for dependency injection) with
I'm trying to understand the best way to map the fields in an Edit
I am trying to research the best way to secure users data. Example: An
I'm trying to understand the best way to implement Python's ConfigParser so that elements
I'm trying to understand what's the best way to manage older unit test that
I'm trying to understand the best options for pulling off a wizard form in

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.