开发者

Cant insert utf8 characters on mysql (with utf8 collation, charset and nameset)

im facing a really stressing problem here.. i have everything in UTF-8 , all my DB and tables are utf8_general_ci but when trying to insert or update from a single PHP script all i see are symbols.. but if i edit in phpmyadmin the words are shown correctly.. i found that if i run the utf8_decode() function to my strings in开发者_开发知识库 php i can make it work, but im not planning to do that because is a mess and it should work without doing that :S

Here is a basic code im using to test this:

<?php
$conn=mysql_connect("localhost","root","root") 
  or die("Error");
mysql_select_db("mydb",$conn) or
  die("Error");
mysql_query("UPDATE `mydb`.`Clients` SET `name` = '".utf8_decode("Araña")."' WHERE `Clients`.`id` =25;", 
   $conn) or die(mysql_error());
mysql_close($conn);
echo "Success.";
?>

This is what i get if i dont decode utf8 with php utf8_decode function:

instead of Araña, i get : Araña


I've run into the same issue many times. Sometimes it's because the type of database link I'm selecting from isn't the same type that I'm using for inserting and other times, it's from file data into a database.

For the later instance, mysql_set_charset('utf8',$link); is the magic answer.

Place the call to mysql_set_charset just after you select your database via mysql_select_db.

@ref http://php.net/manual/en/function.mysql-set-charset.php


"Araña" IS UTF-8. The characters "ñ" represent the two bytes into which the Spanish ñ are encoded in UTF-8. Whatever you're reading it back with is not handling the UTF-8 and is displaying it as (it appears) ISO-8859-1.


That DDL you mentioned has to do with the collation, not the character set. The correct statement would be:

ALTER TABLE Clients CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

You still need to make sure the client library (libmysql or whatever driver PHP is using) is not transcoding the data back to ISO-8859. mysql_set_charset('utf8') will explicitly set the client encoding to UTF-8. Alternatively, you can send a SET NAMES UTF8; right after you connect to the database. To do that implicitly, you can change the my.cnf [client] block to have utf-8 as the client character encoding (and /etc/init.d/mysql reload to apply). Either way, make sure the client doesn't mangle the results it's pulling.

[client] default-character-set=utf8

You do not need to use utf8_decode if you're using mbstrings. The following php.ini configuration should ensure UTF-8 support on the PHP side:

mbstring.internal_encoding = utf-8
mbstring.http_output = utf-8
mbstring.func_overload = 6

Finally, when you display the results in HTML, verify that the page's encoding is explicitly UTF-8.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜