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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:12:24+00:00 2026-06-09T08:12:24+00:00

I’ve written SELECT statement that works perfectly. However, I need to make some changes

  • 0

I’ve written SELECT statement that works perfectly. However, I need to make some changes so that it now gets user submitted information from a form and returns results from the database based on that.

It will essentially be a product search—if a user searches for “red” they’ll get back all items that are red. If they search for “red” and “wood” they’ll get back only the items that are red and made of wood.

Here is my HAVING clause:

HAVING values LIKE "%Red%" AND values LIKE "%Wood%"

If I had a series of 5 drop down menus, each with a different set of terms, should I try to build the HAVING clause dynamically based on what drop downs the user uses? How would this be done?

  • 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-09T08:12:25+00:00Added an answer on June 9, 2026 at 8:12 am

    I would opt for a static SQL statement, given that there are exactly five “options” the user can select from.

    What I would do is use an empty string as the value that represents “no restriction” for a particular option, so my statement would be like this: e.g.

    HAVING `values` LIKE CONCAT('%',:b1,'%')
       AND `values` LIKE CONCAT('%',:b2,'%')
       AND `values` LIKE CONCAT('%',:b3,'%')
       AND `values` LIKE CONCAT('%',:b4,'%')
       AND `values` LIKE CONCAT('%',:b5,'%')
    

    To apply restrictions only on options 1 and 2, and no restriction on option 3, e.g.

    $sth->bind_param(':b1','Red');
    $sth->bind_param(':b2','Wood');
    $sth->bind_param(':b3','');
    $sth->bind_param(':b4','');
    $sth->bind_param(':b5','');
    

    To apply restriction only on option 2, e.g.

    $sth->bind_param(':b1','');
    $sth->bind_param(':b2','Wood');
    $sth->bind_param(':b3','');
    $sth->bind_param(':b4','');
    $sth->bind_param(':b5','');
    

    This allows you to have a static statement, and the only thing that needs to change is the values supplied for the bind parameters.


    It’s also possible to use a NULL value to represent “no restriction” for an option, but you’d need to modify the SQL statement to do a “no restriction” when a NULL value is supplied. Either check for the NULL value specifically, e.g.

    HAVING ( `values` LIKE CONCAT('%',:b1,'%') OR :b1 IS NULL )
       AND ( `values` LIKE CONCAT('%',:b2,'%') OR :b2 IS NULL )
       AND ( `values` LIKE CONCAT('%',:b3,'%') OR :b3 IS NULL )
    

    -or- just convert the NULL to an empty string for use in the LIKE predicate, e.g.

    HAVING `values` LIKE CONCAT('%',IFNULL(:b1,''),'%')
       AND `values` LIKE CONCAT('%',IFNULL(:b2,''),'%')
       AND `values` LIKE CONCAT('%',IFNULL(:b3,''),'%')
    

    The same technique will work with a WHERE clause as well. You may want to consider whether a WHERE clause is more appropriate in your case. (We can’t tell from the information provided.)

    But note that the HAVING clause does not restrict which rows are included in the statement; rather, the HAVING clause only restricts which rows from the result set are returned. The HAVING clause is applied nearly last in the execution plan (I think its followed only by the ORDER BY and LIMIT.

    The HAVING clause can apply to aggregates, which the WHERE clause cannot do. The HAVING clause can reference columns in the SELECT list, which the WHERE clause cannot do.

    (Also note that VALUES is a reserved word, if it’s not qualified (preceded by alias., it may need to be enclosed in backticks.)

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.