Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coldfusion CFQUERY w/ Inner Join & Dateformat Not Working

I am trying to wrap my head around dealing joins, mysql and coldfusion. The following query works without the last condition.

        <cfquery name="GetWeekends">
        SELECT w.id, w.weekend_type, w.community_id, w.start_date, w.end_date,
          w.language,
          c.community_id, c.location, c.language, c.state, c.country
        FROM _weekends w
        INNER JOIN _communities c
        ON w.community_id=c.community_id
        WHERE w.weekend_type = 1 AND w.start_date > Now() AND           
        #DateFormat(w.start_date, "m")# = '#form.home_by_month#'
        ORDER BY w.start_date ASC
        </cfquery>

It is dying on

  #DateFormat(w.start_date, "m")#

telling me variable [W] doesn't exist. Sorry, I am learning as I go here...

like image 987
mck Avatar asked Mar 01 '26 23:03

mck


1 Answers

The issue here is that DateFormat() is a ColdFusion function, and that cannot be applied with a MySQL "variable" (w). You need to use the MONTH() function for MySQL and pass the date into that.

Don't forget to sanitize your form inputs, you are highly susceptible to SQL injection. Use cfqueryparam like so:

MONTH(w.start_date) = <cfqueryparam cfsqltype="cf_sql_integer" value="#form.home_by_month#" />
like image 107
Sterling Archer Avatar answered Mar 03 '26 22:03

Sterling Archer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!