Remove Empty List Items
Today, I discovered that empty list items were causing a query to bomb with the helpful “You have an error in your SQL syntax” message. The SQL attempting to execute was SELECT name, group_id FROM riders WHERE group_id IN (11,,,,,,,,,,,,), where the value 11,,,,,,,,,,,, is generated from a variable called groupidlist.
There are a few ways to deal with this situation. My first instinct was to remove the empty list items from groupidlist. I prefer to clean things up as early as possible, in case this value might be stored or used again somewhere else down the line. I can easily remove the empty items from the list by performing the following:
<cfset groupidlist = arrayToList(listToArray(groupidlist)) />
The listToArray function removes empty list items as a bonus.
The other option, which should be performed as well, is to use cfqueryparam in my SQL statement.
SELECT name, group_id
FROM riders
WHERE group_id IN (<cfqueryparam cfsqltype="CF_SQL_INTEGER" list="Yes" separator="," value="#groupidlist#" />)