Unable to insert utf8 characters in mysql (with utf8 setting, encoding and set of names)

im facing a really tense problem here .. I have everything in UTF-8, all my DBs and tables are utf8_general_ci, but when I try to insert or update from a single PHP script, all that I see are characters .. but if I I edit in phpmyadmin, the words are displayed correctly .. I found that if I run the utf8_decode () function for my lines in php, I can make it work, but I do not plan to do it because it is a mess, and it should work without execution what: S

Here is the basic im code used to verify 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 do not decode utf8 using php function utf8_decode:

instead of Araña, I get: Araà ± a

+4
source share
3 answers

I have come across the same problem many times. Sometimes this is because the type of database link that I select is not the same type that I use to insert, and at other times, it is from data files to the database.

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

Place a call to mysql_set_charset immediately after selecting your database via mysql_select_db.

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

+4
source

"Araà ± a" IS UTF-8. The characters "à ±" are two bytes in which Spanish is encoded in UTF-8. No matter what you read, it does not process UTF-8 and displays it as (it appears) ISO-8859-1.

+1
source

This DDL that you talked about is about sorting, not a character set. Correct statement:

 ALTER TABLE Clients CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; 

You still need to make sure that the client library (libmysql or any other PHP driver) does not encode data back to ISO-8859. mysql_set_charset ('utf8') will explicitly set the client encoding to UTF-8. Alternatively, you can send SET NAMES UTF8; immediately after connecting to the database. To do this implicitly, you can modify the my.cnf [client] block to have utf-8 as the character encoding of the client (and / etc / init.d / mysql reload to apply). In any case, make sure that the client does not distort the results that he pulls.

[client] default-character-set = utf8

You do not need to use utf8_decode if you use mbstrings. The following php.ini configuration should provide 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, make sure the page encoding is explicitly UTF-8.

+1
source

All Articles