简体   繁体   中英

Do I need a new query object for each MySQL statement?

I'm trying to compile several MySQL statements and execute them in the same request, but I keep getting a MySQL syntax error:

local.sql = "";
for (local.i in this.hitArray)
{
    local.sql &= "UPDATE posts SET viewCount = posts.viewCount + 1 WHERE posts.id = #local.i.postId#;";
}
local.service.setSQL(local.sql);

Raw SQL dump (local.sql var):

UPDATE posts SET viewCount = posts.viewCount + 1 WHERE posts.id = 95;UPDATE posts SET viewCount = posts.viewCount + 1 WHERE posts.id = 95;UPDATE posts SET viewCount = posts.viewCount + 1 WHERE posts.id = 95;

It appears the query is chocking right at the end of the first statement.

MySQL disables multiple statements by default to prevent sql injection. You must add the allowMultiQueries flag to your connection string to enable them.

Note: Obviously if you enable multiple statements, it is very important all of your queries use cfqueryparam or addParam because you are now at risk for sql injection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM