简体   繁体   English

如何在latin1数据库中打印UTF-8数据?

[英]How to print UTF-8 data in latin1 database?

My issue is I have a database which was imported as UTF-8 that has columns that are default latin1. 我的问题是我有一个以UTF-8格式导入的数据库,该数据库具有默认为latin1的列。 This is obviously an issue so when I set the charset to UTF-8 on php it gives me instead of the expected ae character. 这显然是一个问题,因此当我在php 字符集设置为UTF-8时,它会给我 而不是预期的ae字符。

Now, when I originally had my encoding as windows-1252 it worked perfectly but then when I validate my file it says that windows-1252 is legacy and shouldn't be used. 现在,当我最初将我的编码设为windows-1252它可以正常工作,但是当我验证文件时,它说windows-1252是旧版的,不应使用。

Obviously I'm only trying to get rid of the error message but the only problem is I'm not allowed to change anything in the database at all. 显然,我只是想摆脱错误消息,但唯一的问题是我根本不允许更改数据库中的任何内容。 Is there any way the data can be output as utf-8 whilst still being stored as latin1 in the DB? 有什么方法可以将数据以utf-8格式输出,同时仍以latin1格式存储在数据库中吗?

Time ago, I used this function to resolve printing texts in a hellish page of different lurking out-of-control charsets xD: 以前,我使用此功能来解决在不同的潜伏字符集xD的地狱页面中打印文本:

function to_entities($string)
{
    $encoding = mb_detect_encoding($string, array('UTF-8', 'ISO-8859-1')); // and a few encodings more... sigh...
    return htmlentities($string, ENT_QUOTES, $encoding, false);          
}

print to_entities('á é í ó ú ñ');

1252 (latin1) can handler æ . 1252(latin1)可以处理æ It is hex E6 . 它是十六进制E6 In utf8 it is hex C3A6 . 在utf8中为十六进制C3A6

usually comes from latin1 encodings, then displaying them as utf8. 通常是处理latin1编码,然后展示他们为UTF8。 So, let's go back to what was stored. 因此,让我们回到存储的内容上。

Please provide SHOW CREATE TABLE . 请提供SHOW CREATE TABLE I suspect it will say CHARACTER SET latin1 , not utf8. 我怀疑它会说CHARACTER SET latin1而不是utf8。

Then, let's see 那我们来看一下

SELECT col, HEX(col) FROM tbl WHERE ...

to see the hex. 看十六进制。 (See hex notes above.) (请参见上面的十六进制注释。)

Assuming everything is latin1 so far, then the simple (and perhaps expedient) answer is to check the html source. 假设到目前为止一切都是latin1,那么简单的(也许是方便的)答案是检查html源。 I suspect it says 我怀疑它说

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Changing to charset=ISO-8859-1 may solve the black diamond problem. 更改为charset=ISO-8859-1可能会解决黑钻石问题。

But... latin1 only handles Western European characters. 但是... latin1仅处理西欧字符。 If you need Slavic, Greek, Chinese, etc, then you do need utf8. 如果您需要斯拉夫文,希腊文,中文等,那么您确实需要utf8。 I'll provide a different answer in that case. 在这种情况下,我将提供不同的答案。

I have figured out how to do this after looking through the link that Fred provided, thanks! 通过查看弗雷德提供的链接,我已经弄清楚了该如何做,谢谢!

if anyone needs to know what to do 如果有人需要知道该怎么做

if you have a database connection file. 如果您有数据库连接文件。 inside that, underneath the mysqli_connect command add 在里面,在mysqli_connect命令下面添加

mysqli_set_charset($connectvar, "utf8");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM