简体   繁体   中英

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! , I can see it correctly in the script that gets the feeds but when it saves in the database it saves like Diseñó! . I have my database set to 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. Anybody has a solution? Thanks!

At a guess, the page that is the source of the "Diseño!" is probably encoded in iso-8859-1 or windows-1252, and it's being stored in your database without any conversion.

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 .

Are you using 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.

To fix this, add encoding=utf8 to the connection parameters in SimplePie_Cache_MySQL.php .

You have another problem too: when you read the database later you are not setting the page encoding to 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.

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.

Bye

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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