Odd MySql CFQueryParam Truncation Issue (Help)?

Posted February 27, 2008 at 11:49 AM by Ben Nadel

Tags: ColdFusion, SQL

Can anyone help me out with this? I am used to using Microsoft SQL Server, which I love; recently, however, I have been working on some MySQL 5.0 databases and they seem to have some odd rules. Right now, I am getting a really strange truncation error when using CFQueryParam in a calculated column. Running this code:

  • SELECT
  • <cfqueryparam
  • value="1234567890123456789012345678901234567890"
  • cfsqltype="cf_sql_varchar"
  • /> AS value

... returns this value in the query:

1234567890123

As you can see, only the first 13 characters get used in the SQL. I have found this error to be more or less consistent no matter what the data. I tried switching from cf_sql_varchar to cf_sql_longvarchar but there was no improvement. How can I get MySQL to stop truncating my CFQueryParam error? It works when I take out the ColdFusion CFQueryParam tag, but I always feel uncomfortable not using the CFQueryParam tag - I feel like it adds a little bit more of a security comfort blanket so that no matter what data I throw at it, I know the type will be validated.

So, how do I keep the CFQueryParam tag and lose the truncation (short of forcing all clients to use MS SQL Server)?



Reader Comments

Feb 27, 2008 at 11:54 AM // reply »
3 Comments

Whats the field definition in MySQl? Just making sure that its not varchar(13) ;)


Feb 27, 2008 at 11:57 AM // reply »
14 Comments

Don't think the problem is with field definition as Ben isn't querying any field there...


Feb 27, 2008 at 11:57 AM // reply »
10,640 Comments

@Stephen,

It's a calculated column - there is no preexisting definition.


Feb 27, 2008 at 12:37 PM // reply »
3 Comments

hmm well... its got to be down to the bind created by cfqueryparam and the jdbc drivers.

Though why it would be different when you used cfqueryparam as value rather than as part of a condition I'm afraid I don't know. Have to be honest, while I use cfqueryparam against MySQL 5 conditions all the time - I've never had to use it as a part of a calculated value.


Feb 27, 2008 at 12:40 PM // reply »
10,640 Comments

@Stephen,

Maybe I just have to get over my fear of non-CFQueryParam and dynamic data and just get rid of it for those scenarios. This might just be a limitation in MySQL that doesn't exist in SQL Server... of course, I never really know which is the limitation of the server and which is a limitation of the driver.


Feb 27, 2008 at 1:53 PM // reply »
1 Comments

What version of MYSQL/CF are you using? I can not replicate this on MYSQL 5.0.19 and CF 7,0,1,116466 and JConnector 3.1.12 as the driver.


Feb 27, 2008 at 2:41 PM // reply »
10,640 Comments

@Jason,

I know we have some flavor of MySQL 5 and I think whatever the default MySQL drive in ColdFusion is. Sorry, I don't know enough about the setup. If you can't reproduce it, I guess it might just be an outdated driver on our end.


Feb 27, 2008 at 2:51 PM // reply »
2 Comments

Hi Ben,
For what it's worth, I'm using mySQL 5 and CF8 and I'm not experiencing that issue either.


Feb 27, 2008 at 3:15 PM // reply »
10,640 Comments

@Glenn,

Thanks... I guess it's just our configuration.


Feb 28, 2008 at 10:48 AM // reply »
21 Comments

I'm not sure I understand where the value being passed to cfqueryparam is coming from? Is it calculated in CF or in SQL?

Either way, it raises the philosophical question: if you're not passing values directly from the user to the SQL statement, do you really need to use cfqueryparam?

The value of cfqueryparam in this situation is pretty obvious:
<cfqueryparam value="#form.value#">

But my colleagues routinely write code like this:

<cfset bit = 1>
<cfqueryparam value="#bit#">

or even this:

<cfqueryparam value="1">

IMHO, this is unnecessary. But I'd be curious to hear what other developers think about this.


Feb 28, 2008 at 10:56 AM // reply »
10,640 Comments

@David,

The value is coming from a user submitted form. The use case is a bit kloogy though. I am, in a way, misusing the query. I am using a SQL call to create a "report criteria" query with columns Name, and Value. Something like (but not quite):

SELECT
(
'Date From' AS name,
<cfqueryparam value="#DateFormat( FORM.date_from )#" /> AS value
)

UNION ALL

(
'Date To' AS name,
<cfqueryparam value="#DateFormat( FORM.date_to )#" /> AS value
)

...etc...

This gives me the ability to pass over a query to my report rendering code which can easily loop over it and output name/value pairs.

So, looking at that, you might be wondering why I even use a SQL call? Why not just create the query manually and populate it? The reason is that with things like date to/from, the value is clear. However, what about if my report criteria is an ID? Ex. client_id? In that case, I use the SQL query to get that value:

(
'Client' AS name,
(
SELECT c.name FROM client c WHERE c.id = <cfqueryparam ... />
) AS value
)

This way, for the most part, I can group all my criteria construction into one place.

So, where does CFQueryParam come into play? I use it for the "value" bindings since I feel that to a certain degree, it is user entered. Is it truly required? Not really, but I like to use it out of habit.

As far as the use of CFQueryParam with static values like is_active = 1, I used to be guilt of that :) I have since gotten rid of that. All hard coded values are now just plainly entered into the code. Ideally, I only want to use CFQueryParam where it adds value. Maybe this example (the criteria query) is not one of those cases.


Feb 28, 2008 at 9:11 PM // reply »
7 Comments

try taking out cfsqltype="cf_sql_varchar" and just use value.


Feb 28, 2008 at 9:47 PM // reply »
28 Comments

Ben, have you tried using the maxlength attribute of cfqueryparam?


Feb 3, 2010 at 9:05 AM // reply »
3 Comments

I am getting a lot of errors trying to use <CFQUERYPARAM> with mySQL. The error I am getting is this: "Communication link failure: Unknown command"


Feb 3, 2010 at 9:07 AM // reply »
10,640 Comments

@Adrian,

That doesn't sound good. Does the database communication work with fewer query params?


Feb 3, 2010 at 9:45 AM // reply »
3 Comments

Actually it works very weird. I think the error shows up only when using CF_SQL_CHAR... I am not sure yet. I have to do more debuging. Anyhow, for the moment I just removed <CFQUERYPARAM> tags and just use the values.


Feb 4, 2010 at 9:49 PM // reply »
10,640 Comments

@Adrian,

I know there are some compatibility issues with some of the cfSqlType values. For instance, some databases support CF_SQL_DATE and some don't. I believe the ColdFusion documentation somewhere touches on this. Perhaps, for some reason, your driver simply doesn't translate the CHAR well. Try replacing it with VARCHAR in the CFQueryParam?



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »