传统的ASP,MySQL或ODBC UTF8编码 [英] Classic ASP, MySQL or ODBC UTF8 encoding

查看:612
本文介绍了传统的ASP,MySQL或ODBC UTF8编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与GoDaddy主办,包括在后端MySQL数据库的网站。该网站是一个斯洛文尼亚网站,所以特殊字符使用。

I have a website hosted with GoDaddy, including MySQL database on the back end. The site is a Slovenian site, so special characters are used.

该网站建于传统的ASP和我在记事本+ +在使用UTF-8编码创建的所有页面。在每一个页面的顶部我也有会话。codePAGE = 65001,Session.LCID = 1060和Response.Charset的=UTF-8。 MySQL数据库和所有的表都还UTF8 EN codeD。

The website is built in classic ASP and I have all the pages created in Notepad++ where utf-8 encoding is used. At the top of every page I also have Session.CodePage=65001, Session.LCID=1060 and Response.Charset="utf-8". MySQL db and all the tables are also utf8 encoded.

如果我在看数据直接通过数据库接口工作台,一切都很好,包括一些特殊字符斯洛文尼亚语我用,如:C

If I look at the data directly in db through Workbench interface, everything is ok, including some special Slovenian characters I use, like: č

如果我去我的网站,斯洛文尼亚人物也都印得很好,包括C

If I go to my website, the Slovenian characters are also printed just fine, including č

唯一的问题是,在同一页上,从MySQL retrived数据没有codeD正确,所以字母C becommes?

The only problem is, that on the same page, data retrived from MySQL is not coded correctly, so letter č becommes ?

这可能是什么问题,该如何解决呢?

What could be the problem and how to solve it?

首先,我认为这是MySQL的ODBC驱动程序3.51,我用它来连接到数据库。我曾尝试加入的charset = UTF8到连接字符串,但没有奏效。我也尝试添加的charset = UCS2连接字符串,这是一个提示,我在其他网站上找到,但它并没有帮助。 GoDaddy的是不支持的MySQL ODBC 5.1驱动,这可能是一个解决办法。

First I thought it is the MySQL ODBC 3.51 Driver, which I use to connect to db. I have tried adding charset=utf8 to the connection string, but didn't work. I have also tried adding charset=ucs2 to the connection string, which is a tip I found on another website, but it didn't help either. GoDaddy is not supporting MySQL ODBC 5.1 Driver, which could be a solution.

我跑出来的选择,所以请大家帮忙。

I am running out of options, so please help.

推荐答案

您必须根据本<斯洛文尼亚字母机会A HREF =HTTP://www.uni$c$c.org/Public/MAPPINGS/ VENDORS / MICSFT / WindowsBestFit / bestfit1252.txt相对=nofollow>映射并从的 Windows的1252 wiki文章

根据微软和统一code联盟的网站的信息,
  位置81,8D,8F,90,和9D是未使用的;然而,在Windows API
  的MultiByteToWideChar 这些映射到相应的 C1控制codeS

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 MultiByteToWideChar maps these to the corresponding C1 control codes.

在80位的欧洲字符是不是在早期版本中此code页面present,
  也没有人S,S,Z,并与卡隆(HACEK)z的。

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).

这里的事情要做:

Here's the things to do:


  1. 使用UTF-8(无BOM)EN codeD文件对含有硬codeD文字的可能性。 (✔已经完成)

  1. Use UTF-8 (without BOM) encoded files against the possibility of contain hard-coded text. (✔ already done)

指定的服务器端或与客户端的meta标签与ASP响应字符集UTF-8。 (✔已经完成)

Specify UTF-8 for response charset with ASP on server-side or with meta tags on client-side. (✔ already done)

告诉MySQL服务器的命令在字符集UTF-8,以及你期望UTF-8 EN codeD结果集。添加一个初始语句连接字符串: ...;语句= SET NAMES'UTF8'; ...

Tell the MySQL Server your commands are in charset utf-8, and you expect utf-8 encoded result sets. Add an initial statement to the connection string : ...;stmt=SET NAMES 'utf8';...

响应。codePAGE设置为1252。

Set the Response.CodePage to 1252.

我测试过下面的脚本,它就像一个魅力。

I've tested the following script and it works like a 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 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.HTMLEn code再因响应。codePAGE是1252服务器端,自Server.HTMLEn code是依赖于上下文codePAGE这将导致乱码输出。

所以你需要编写自己的HTML连接codeR来处理这种情况。

When you need to apply html encoding to strings fetched from the database, you shouldn't use Server.HTMLEncode anymore due to Response.Codepage is 1252 on server-side and since Server.HTMLEncode is dependent context codepage this will cause gibberish outputs.
So you'll need to write your own html encoder to handle the 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)

这篇关于传统的ASP,MySQL或ODBC UTF8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆