utf 8 - Classic ASP, MySQL or ODBC UTF8 encoding -


i have website hosted godaddy, including mysql database on end. site slovenian site, special characters used.

the website built in classic asp , have pages created in notepad++ utf-8 encoding used. @ top of every page have session.codepage=65001, session.lcid=1060 , response.charset="utf-8". mysql db , tables utf8 encoded.

if @ data directly in db through workbench interface, ok, including special slovenian characters use, like: č

if go website, slovenian characters printed fine, including č

the problem is, on same page, data retrived mysql not coded correctly, letter č becommes ?

what problem , how solve it?

first thought mysql odbc 3.51 driver, use connect db. have tried adding charset=utf8 connection string, didn't work. have tried adding charset=ucs2 connection string, tip found on website, didn't either. godaddy not supporting mysql odbc 5.1 driver, solution.

i running out of options, please help.

you have chance slovenian letters according mapping , excerpt windows-1252 wiki article:

according information on microsoft's , unicode consortium's websites, positions 81, 8d, 8f, 90, , 9d unused; however, windows api multibytetowidechar maps these corresponding c1 control codes.

the euro character @ position 80 not present in earlier versions of code page, nor s, s, z, , z caron (háček).

here's things do:

  1. use utf-8 (without bom) encoded files against possibility of contain hard-coded text. (✔ done)

  2. specify utf-8 response charset asp on server-side or meta tags on client-side. (✔ done)

  3. tell mysql server commands in charset utf-8, , expect utf-8 encoded result sets. add initial statement connection string : ...;stmt=set names 'utf8';...

  4. set response.codepage 1252.

i've tested following script , works charm.

ddl: http://sqlfiddle.com/#!8/c2c35/1

asp:

<%@language=vbscript%> <%  option explicit  response.codepage = 1252 response.lcid = 1060 response.charset = "utf-8"  const adcmdtext = 1, advarchar = 200, adparaminput = 1, adlockoptimistic = 3  dim connection set connection = server.createobject("adodb.connection")     connection.open "driver={mysql odbc 3.51 driver};server=localhost;database=mydb;user=myusr;password=mypwd;stmt=set names 'utf8';"  if request.form("name").count = 1 , len(request.form("name")) 'add new     dim rsadd     set rsadd = server.createobject("adodb.recordset")         rsadd.open "names", connection, ,adlockoptimistic         rsadd.addnew         rsadd("name").value = left(request.form("name"), 255)         rsadd.update         rsadd.close     set rsadd = nothing end if  dim command set command = server.createobject("adodb.command")     command.commandtype = adcmdtext     command.commandtext = "select name `names` order id desc"      if request.querystring("name").count = 1 , len(request.querystring("name"))         command.commandtext = "select name `names` name = ? order id desc"         command.parameters.append command.createparameter(, advarchar, adparaminput, 255, left(request.querystring("name"), 255))     end if      set command.activeconnection = connection     command.execute         while not .eof             response.write "<a href=""?name=" & .fields("name").value & """>" & .fields("name").value & "</a><br />"             .movenext         wend         .close     end      set command.activeconnection = nothing     set command = nothing  connection.close %><hr /> <a href="?">show all</a><hr /> <form method="post" action="<%=request.servervariables("script_name")%>"> name : <input type="text" name="name" maxlength="255" /> <input type="submit" value="add" /> </form> 

as last remark:

when need apply html encoding strings fetched database, shouldn't use server.htmlencode anymore due response.codepage 1252 on server-side , since server.htmlencode dependent context codepage cause gibberish outputs.
you'll need write own html encoder handle case.

function myownhtmlencode(byval str)     str = replace(str, "&", "&amp;")     str = replace(str, "<", "&lt;")     str = replace(str, ">", "&gt;")     str = replace(str, """", "&quot;")     myownhtmlencode = str end function 'response.write myownhtmlencode(rs("myfield").value) 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -