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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:50:37+00:00 2026-06-03T22:50:37+00:00

I inherited a coldfusion8 site, which is using some search functionality. I’m trying to

  • 0

I inherited a coldfusion8 site, which is using some search functionality.

I’m trying to store standardized database querys into stored procedures. Since I’m a newbie on both Coldfusion and MySQL I’m now wondering whether I can do this in MySQL:

<cfquery datasource="db" name="find_cats">
    SELECT wg.no, wg.type, wg.keywords, wg.lang
    FROM cats AS wg 
    <cfloop list="searchForm.cats_search_string" delimiters=", " item="tag">
      WHERE wg.keywords LIKE <cfqueryparam value='%#tag#%' cfsqltype='cf_sql_varchar'> AND
    </cfloop>
    wg.lang = <cfqueryparam value="#Session.lang#" cfsqltype="cf_sql_varchar">
</cfquery>
<cfset cond_cats = "AND (1=2">
    <cfoutput query="find_cats">
        <!--- check if found category belongs to either AAA or BBB classifcation --->
        <cfif wg.type is "AAA">
            <cfset cond_cats = cond_cats & " OR categoryID1 = #wg.no#">
        </cfif>
        <cfif wg.typ is "BBB">
            <cfset cond_cats = cond_cats & " OR categoryID2 = #wg.no#">
        </cfif>
    </cfoutput>
    <cfset cond_cats = cond_cats & ")">

The search can either be keyword or index (AAA, BBB) based. I’m still trying to understand what is happending, but so far I think if the user enters a string like:

 string1, string2, string3 string4

The first part loops through the four strings (delimiter comma and space) and querys the database for matching keywords. It then creates a new cond_cats variable, which is used when the actual search takes place. I guess it replaces the search strings, with matching categories, but I’m not sure here, as the result will be

AND (1=2 OR category1 = 12345 OR category2 = 88888 )

Which is appended to the actual search query.

My questions:
Is there a way in MySQL to split the user-entered search string, so I’m able to run the loop? The second part should be an out-parameter of the stored procedure, shouldn’t it? If this is run before every search, should it be a stored procedure at all or should I keep using a database query?

Thanks for some input!

  • 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-03T22:50:39+00:00Added an answer on June 3, 2026 at 10:50 pm

    From what you described, I am not seeing much benefit to wrapping it in a stored procedure/function. Databases are not really optimized for string manipulation. While looping is possible, the equivalent sql code is usually far less elegant. Plus it may require the use of dynamic sql (or a temp table to do it safely).

    However, looking over your code I believe you could eliminate the second cfloopaltogether by using a single IN (....) clause instead of constructing a series OR conditions. (The two are equivalent.)

    Just modify the first query to retrieve only categories for the desired types ie “AAA” and “BBB”.

    <cfquery datasource="db" name="find_cats">
        SELECT  wg.no
        FROM    cats AS wg 
        WHERE   wg.lang = <cfqueryparam value="#Session.lang#" cfsqltype="cf_sql_varchar">
        AND     wg.type IN ('AAA', 'BBB')
        AND  ( 1 = 2
             <cfloop list="#searchForm.cats_search_string#" delimiters=", " item="tag">
                  OR wg.keywords LIKE <cfqueryparam value='%#tag#%' cfsqltype="cf_sql_varchar"> 
              </cfloop>
           )
    </cfquery>
    

    Then use ValueList to feed the results into your second query. It is possible you may even be able to merge your two queries into one. But that all depends on the complexity of your search query.

    WHERE categoryID1 IN 
    ( 
        <cfqueryparam value="#valueList(find_cats.no)#" list="true" ... > 
    )
    

    EDIT: I think you could still use valueList. Just add a CASE statement to the first query that separates the matching values into two columns:

    SELECT  wg.no
            , CASE WHEN wg.type = 'AAA' THEN wg.no ELSE NULL END AS ID1Values
            , CASE WHEN wg.type = 'BBB' THEN wg.no ELSE NULL END AS ID2Values
    ...
    

    Then generate a list of the values in each column:

         <cfset firstList  = valueList(find_cats.ID1Values)>
         <cfset secondList = valueList(find_cats.ID2Values)>
    

    Finally use those lists within your search query.

       WHERE 1 = 2
    
       <cfif listLen(firstList)>
             OR categoryID1 IN ( <cfqueryparam value="#firstList#" list="true" cfsqltype="cf_sql_integer"> )
       </cfif>
    
       <cfif listLen(secondList)>
             OR categoryID2 IN ( <cfqueryparam value="#secondList#" list="true" cfsqltype="cf_sql_integer"> )
       </cfif>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I inherited an ASP.NET MVC 1 website using MVCContrib 1.5 that I'm trying to
I inherited a database from an old system we have, in which (among other
I inherited a new system and I am trying to make some improvements on
Just inherited a large PHP 5.3 site and wondered if there were some sort
I inherited a data-storage which was using simple text-files to save documents. Documents had
I inherited an Access application that I have made some changes to. When I
I inherited this gigantic legacy Java web app using Struts 1.2.4. I have a
I inherited this gigantic legacy Java web app using Struts 1.2.4. I have a
I inherited a database built with the idea that composite keys are much more
I inherited a system that stores default values for some fields in some tables

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.