经典ASP,MySQL或ODBC UTF8编码

我有一个托管GoDaddy的网站,包括后端的MySQL数据库.该网站是斯洛文尼亚网站,因此使用了特殊字符.

该网站是用经典ASP构建的,我在Notepad中创建了所有使用utf-8编码的页面.在每个页面的顶部,我还有Session.CodePage = 65001,Session.LCID = 1060和Response.Charset =“utf-8”. MySQL db和所有表也都是utf8编码的.

如果我通过Workbench界面直接在db中查看数据,一切正常,包括我使用的一些特殊的斯洛文尼亚字符,如:č

如果我去我的网站,斯洛文尼亚字符也打印得很好,包括č

唯一的问题是,在同一页面上,从MySQL重新获得的数据编码不正确,所以字母be becommes?

可能是什么问题以及如何解决?

首先我认为它是MySQL ODBC 3.51驱动程序,我用它连接到db.我已经尝试将charset = utf8添加到连接字符串,但是没有用.我也尝试将charset = ucs2添加到连接字符串,这是我在另一个网站上找到的提示,但它也没有帮助. GoDaddy不支持MySQL ODBC 5.1驱动程序,这可能是一个解决方案.

我的选项用完了,所以请帮忙.

解决方法:

根据这个mappingWindows-1252 wiki article的摘录你有机会获得斯洛文尼亚字母:

According to the information on Microsoft’s and the Unicode Consortium’s websites,
positions 81, 8D, 8F, 90, and 9D are unused; however, the Windows API
07002 maps these to the corresponding 07003.

The euro character at position 80 was not present in earlier versions of this code page,
nor were the S, s, Z, and z with caron (háček).

这是要做的事情:

>使用UTF-8(无BOM)编码文件,以防止包含硬编码文本. (✔已经完成)
>在服务器端使用ASP或在客户端使用元标记为响应字符集指定UTF-8. (✔已经完成)
>告诉MySQL服务器你的命令是字符集utf-8,你期望utf-8编码的结果集.在连接字符串中添加一个初始语句:…; stmt = SET NAMES’utf8′; …
>将Response.CodePage设置为1252.

我测试了以下脚本,它就像一个魅力.

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 And Len(Request.Form("name")) Then '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 From `names` Order By id Desc"

    If Request.QueryString("name").Count = 1 And Len(Request.QueryString("name")) Then
        Command.CommandText = "Select name From `names` Where name = ? Order By id Desc"
        Command.Parameters.Append Command.CreateParameter(, adVarChar, adParamInput, 255, Left(Request.QueryString("name"), 255))
    End If

    Set Command.ActiveConnection = Connection
    With Command.Execute
        While Not .Eof
            Response.Write "<a href=""?name=" & .Fields("name").Value & """>" & .Fields("name").Value & "</a><br />"
            .MoveNext
        Wend
        .Close
    End With

    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>

作为最后一句话:

当你需要将html编码应用于从数据库中提取的字符串时,你不应该再使用Server.HTMLEncode,因为Response.Codepage在服务器端是1252,而且由于Server.HTMLEncode是依赖的上下文代码页,这将导致乱码输出.
所以你需要编写自己的html编码器来处理这个案例.

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)
上一篇:WIN7 64位下ODBC的配置


下一篇:python – PypyODBC参数:[ODBC Microsoft Access Driver]参数太少.预计4