PHP / MySQL:将数据插入数据库字符集问题 [英] PHP/MySQL: Insert data into database character set problem

查看:119
本文介绍了PHP / MySQL:将数据插入数据库字符集问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个网站,可从其他网页撷取文字,并将其插入资料库。



问题是所有特殊字元都储存在资料库使用HTML编码,所以我需要转换输出使用:



< meta http-equiv =content-typecontent = text / html; charset = ISO-8859-1/>



我现在的意思是,字符'html版本&#x27; 保存在数据库中。当保存西班牙字符或其他特殊字符时,也会发生这种情况。代替ejample的字母ñ,我得到& ntilde; 保存。



数据库中的空间,我需要以后转换输出使用内容类型所以:



我如何才能转换或设置字符集之前保存或只是让MySQL convert it ??



如果你需要知道我如何连接到数据库:

  function dbConnect(){
$ conn = new mysqli(DB_SERVER,DB_USER,DB_PASSWORD,DB_NAME)
return $ conn;
}

$ conn = dbConnect();
$ stmt = $ conn-> stmt_init();

希望你能帮助我!
谢谢。

解决方案

您可以使用 html_entity_decode() 从HTML转换为(真实)字符编码。

 < ;? echo html_entity_decode(& ntilde;,ENT_COMPAT,UTF-8); > 
ñ

请注意,HTML不是通常意义上的字符编码,因此 iconv 等库不能理解,也不能由MySQL本身理解。



(上面的例子),使整个应用程序使用UTF-8。单字符编码,如ISO8859现在已经被有效地废弃,因为Unicode被广泛支持。


I'm building a website that fetches text from another page and insert it into the database.

The problem is that all the special characters are saved in the database using the HTML encoding so then I need to convert the output using:

<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />

I mean, what I have right now is instead of just saving the character " ' " the html version " &#x27; " is saved in the database. This happens also when spanish characters or another special ones are saved. Instead of the letter " ñ " for ejample, I get " &ntilde; " saved.

This wastes space in the database and also I need to later convert the output using content-type so:

How can I just convert or set the charset before is saved or just let MySQL convert it??

In case you need to know here's how I connect to the database:

function dbConnect() {      
    $conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die ('Error.');
    return $conn;
}

    $conn = dbConnect();
    $stmt = $conn->stmt_init();

Hope you can help me!! Thanks.

解决方案

You can use html_entity_decode() to convert from HTML to a (real) character encoding.

<? echo html_entity_decode("&ntilde;", ENT_COMPAT, "UTF-8"); ?>
ñ

Please note that "HTML" isn't a character encoding in the usual sense, so isn't understood by libraries such as iconv, nor by MySQL itself.

I'd also recommend (per example above) having the whole application use UTF-8. Single character encodings such as ISO8859 are effectively obsolete now that Unicode is so widely supported.

这篇关于PHP / MySQL:将数据插入数据库字符集问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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