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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:42:33+00:00 2026-06-17T17:42:33+00:00

I am trying to make a query that will take the inputted items and

  • 0

I am trying to make a query that will take the inputted items and search the database. I know how I have done it several times, but this query just does not want to work.

This is the query

$getcamp = $db->query("SELECT * FROM `campaigns` 
WHERE `requirements` LIKE '%$keyw%' OR `description` LIKE '%$keyw%' OR `name` 
LIKE '%$keyw%' OR `countries` LIKE '%$country%' OR `id` LIKE '%$camp%' OR `category` 
LIKE '%$cat%' AND `active` = '1' ORDER BY `added`") 

or die($db->error);

There are a lot of OR breaks, and I have tried wrapping then in parenthesis in several different variations, but the query still will not work, and there are no errors, it just still shows everything in table instead of what was searched. I am not the best with these kinds of queries as normally I am only writing to search 1 or 2 items, this one is searching for a lot.

If I were to only be searching for the first 3 in that statement it will work fine, but once I start adding onto it is when it breaks, even if I put parenthesis around the first 3, I am just not sure what I am doing wrong.

Also just a side note my host doesn’t have mysqlnd support that’s why I am not using a prepared statement but my variables are escaped before hand.

  • 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-06-17T17:42:35+00:00Added an answer on June 17, 2026 at 5:42 pm

    If one of your variables is empty, it will select all the rows.

    You should do the following (this is a basic example so you understand the idea):

    $orArray = array();
    $orQuery = "";
    if (!empty($keyw) && strlen($keyw) > 2) { // and other SQL injection checks
        $orArray[] = "`requirements` LIKE '%{$keyw}%' ";
    }
    
    if (!empty($orArray())) {
        $orQuery = implode(' OR ', $orArray);
    }
    
    $query = "SELECT * FROM `campaigns` WHERE ( {$orQuery} ) AND active = 1 "; 
    // You also should check if $orQuery is empty, if it is, you need to remove AND. 
    // Well, in that case you wouldn't do the query at all.
    

    Something like this should do, but you need to make the checks for every variable.

    PS: I recommend to set a minimum for string search (3 is a good starting point).

    PS2: This may not be the problem, but I’m pointing it out because it’s a flaw in your scripts logic.

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

Sidebar

Related Questions

I am trying to make a simple query that will update a user database
I am trying to make a query that will insert multiple values from my
I'm trying to make a query that will check for duplicates, and if I
I'm trying to make a Teradata SQL query that will return the n -th
hi i am trying to make a query that will run records from a
I'm trying to make a Linq query that will retrieve a string value from
I am trying to make a query in Codeigniter that will allow a user
I'm trying to make a form that will take given text and direct you
I'm currently trying to write a query that will return all users that have
Ok, I have a SQL query that I'm trying to generate that will combine

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.