I have a function for parameterized SQL entries in ASP Classic that's working great most of the time. The problem is that it's not handling special characters such as å, ä and ö properly. It simply cuts off the string where those characters are. I have set UTF-8 in all ASP-files involved with codepage and charset set and in the database I am using UTF8mb4_unicode_ci and in some cases UTF8mb4_swedish_ci. UTF8 should be working as intended and this function is the only thing at the moment that doesn't seem to handle it. I have checked and the parameter that I'm sending in contains the characters correctly inside the function, both before and after the actual execution.
Here is the function I'm using:
function dbPara(sqlPara, params)
dim cmd
set cmd = Server.CreateObject("ADODB.Command")
with cmd
.CommandText = sqlPara
set .ActiveConnection = cn
end with
if NOT isEmpty(params) then
set rsPara = cmd.execute(, params)
else
set rsPara = cmd.execute()
end if
response.write(params(0)) 'For test purpose only, this will output the entire string.
set dbPara = rsPara
end function
And here's the code of a not properly working example using the function, and one working where it's not parameterized:
Set cn = Server.CreateObject("ADODB.Connection")
cn.Open "Driver={MySQL ODBC 8.0 Unicode driver};Database="&db&";Server="&dbserver&";UID="&dbuser&";PWD="&dbpassword&""
comment = "This is a test to see if å is working properly."
sqlA = "UPDATE orders SET orders_comment = ? WHERE orders_id = 1234"
call dbPara(sqlA, array(comment))
'This will only set the orders_comment to everything up to the å
cn.execute("UPDATE orders SET orders_comment = '" & comment & "' WHERE orders_id = 1234)
'This will set the orders_comment correctly to the whole string
Could anyone see any potential problems with this function causing this behaviour?
EDIT:
Adding an example with more than one entry to explain my comment about the first variable having special characters replaced with ?, but second variable getting its entry cut off. This is when I set the connection string to use charset=utf8;
when charset=ucs2;
as suggested by Lankymark didn't work:
langSQL = "UPDATE faq_entry_lang SET faq_entry_lang_title = ?, faq_entry_lang_content = ? WHERE faq_entry_lang_entryID = ? AND faq_entry_lang_lang = ?"
call dbPara(langSQL,array(faqtitle, faqcontent, entryid, lang))
EDIT 2:
After more testing. It seems random when it cuts off and when it does not using utf8
in the connection string. All special characters that are not cut off will turn into ? though.