简体   繁体   English

西班牙语字符未正确保存在数据库中

[英]Spanish characters not saving in database correctly

I'm going through some feeds of spanish blogs and saving them in a database. 我正在浏览一些西班牙博客供稿,并将它们保存在数据库中。 For example, the word: Diseño! 例如,单词: Diseño! , I can see it correctly in the script that gets the feeds but when it saves in the database it saves like Diseñó! ,我可以在获取提要的脚本中正确看到它,但是当它保存在数据库中时,它会像Diseñó!一样保存Diseñó! . I have my database set to utf8 . 我的数据库设置为utf8。 I think i've followed every single question that looks like this but nothing fix it. 我认为我已经关注了每个看起来像这样的问题,但没有任何解决办法。 I have changed the charset in my html from utf8 to iso-8859 but still, i can see it correctly in the html but not once it saves to the database. 我已经将html中的字符集从utf8更改为iso-8859,但仍然可以在html中正确看到它,但是一旦将其保存到数据库中就不能了。 Anybody has a solution? 有人有解决办法吗? Thanks! 谢谢!

At a guess, the page that is the source of the "Diseño!" 猜测是“Diseño!”的来源页面。 is probably encoded in iso-8859-1 or windows-1252, and it's being stored in your database without any conversion. 可能是用iso-8859-1或Windows-1252编码的,没有任何转换就存储在数据库中。

If that's the case, you need to translate the string from the encoding it's in to utf-8, using something like http://php.net/manual/en/function.mb-convert-encoding.php . 在这种情况下,您需要使用http://php.net/manual/en/function.mb-convert-encoding.php之类的字符串将字符串从其编码转换为utf-8。

Are you using SimplePie_Cache_MySQL ? 您正在使用SimplePie_Cache_MySQL吗? It seems to have a bug, it doesn't set the encoding for the database connection. 似乎有一个错误,它没有设置数据库连接的编码。 This means that the connection encodes data silently from latin1 (the default) to utf8 even when the data is already in utf8. 这意味着即使数据已经在utf8中,该连接也将数据从latin1(默认值)静默编码到utf8。

To fix this, add encoding=utf8 to the connection parameters in SimplePie_Cache_MySQL.php . 若要解决此问题,请在SimplePie_Cache_MySQL.php的连接参数中添加encoding=utf8

You have another problem too: when you read the database later you are not setting the page encoding to utf-8. 您还有另一个问题:稍后读取数据库时,您没有将页面编码设置为utf-8。 This means that correctly encoded data is shown munged. 这意味着正确编码的数据显示为杂音。

Update : On a closer look, it seems that SimplePie_Cache_MySQL is OK, the problem must be elsewhere. 更新 :仔细观察,似乎SimplePie_Cache_MySQL可以,问题必须在其他地方。

You should set the character encoding before establishing the connection to the DB. 建立与数据库的连接之前,应设置字符编码。

You can use this: 您可以使用此:

mysql_set_charset( 'utf-8' );

You can also check this in php.net and follow the recommended links for alternatives like mysqli or pdo. 您也可以在php.net中进行检查并按照推荐的链接查找mysqli或pdo之类的替代方法。

Bye 再见

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

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