Emoji data retrieved via ODBC Connection appears as question mark

北城以北 提交于 2020-01-06 02:38:23

问题


I connect to MySQL version 5.5.50-log from classic ASP pages.

The MySQL Database is set up as follows:

  • Database charset: utf8mb4
  • Database collation: utf8mb4_general_ci

The table and field:

  • Character Set: utf8mb4
  • Collation: utf8mb4_general_ci

To test, I have data in the table, which contains this string: 🍔(T_T) é, è, à, ç

This is a screenshot of the data from the SQLyog:

This is my test web page:

	<!DOCTYPE html>
	
	<html lang="en">
		<head>
			<meta charset="utf-8">
			<meta http-equiv="X-UA-Compatible" content="IE=edge">
			<meta name="viewport" content="width=device-width, initial-scale=1.0">
			<title>Test</title>
			<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
		</head>
		<body>
		<p>Pasted directly from database field: 🍔(T_T) é, è, à, ç</p>
		<p>Returned from SQL statement: ?(T_T) é, è, à, ç</p>
		</body>
	</html>

The issue is not that emoji data cannot be displayed on the web page, as the directly pasted content from MySQL appears fine. The issue is that once the data is returned from an SQL Select from MySQL via the ODBC Driver, it does not render correctly.

I have the following set in the ASP code:

Response.CodePage = 65001
Response.CharSet = "utf-8"

I have tried other variations - e.g.

Response.CodePage = 1252
Response.LCID = 1060
Response.Charset = "utf-8"

But they made no difference.

I have tried a range of MySQL ODBC Drivers - e.g.

''----------------------
''connection string
''----------------------

Dim oConn
set oConn = Server.CreateObject("ADODB.Connection")
'oConn.Open "DRIVER={MySQL ODBC 5.1 Driver}; Server=localhost; Database=mydb; User=root; Password=testing; Option=3; CharSet=utf8mb4; stmt=SET NAMES 'utf8mb4';"

That doesn't work...

I have tried using a System DSN instead using these drivers:

  • MySQL ODBC 5.1 Driver
  • MySQL ODBC 8.0 Unicode Driver
  • MySQL ODBC 8.0 ANSI Driver

None of them solve the issue.

I wondered if there is any way around this, or if there is basically no way to display emoji data that is retrieved from a MySQL database using Classic ASP?

As far as I know, from reading elsewhere, the MySQL database is set up correctly, in a way which supports storing emoji characters, confirmed by the fact I can view it stored in the database without an issue. It's just when I try and pull it out of the database via the Classic ASP to MySQL connection that the emoji character is not displayed.

Edit - the ASP page itself is saved as a UTF-8 encoded file:


回答1:


On top of setting the responses and saving the file as UTF-8, you may also need to set the page language like this. Note that this should be at the very top of the file before anything else:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
response.codepage = 65001
response.charset = "utf-8"
%>

I've fought a lot with encoding and half our system is made up of non-UTF files and this code right here is what has made me able to work with UTF-8 for my own functions.



来源:https://stackoverflow.com/questions/58943772/emoji-data-retrieved-via-odbc-connection-appears-as-question-mark

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!