简体   繁体   中英

PHP PDO Database Error Special Characters

i've read most of the questions and answers about this situation but i cant fix my character problem. My database's default character set is utf8 and all the tables' collation is utf8_general_ci. I'm sure that all of the settings are utf8 and utf8_general_ci, cuz i've checked them billions of time. Problem is after posting the value within a form, it doesnt seem like what i want in database, and also if i edit the database from phpmyadmin, when i fetch the data, its again not showing what i want.

The DB connection works, i edited it like mentioned before about this situation, but my script is buggy about character speciziliation..

The DB Connect Code is :

try {

$db = new PDO("mysql:host={$db_server};dbname={$db_name};charset=utf-8", $db_user, $db_password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
// exc 
}

Would be glad if you can help

Regards

For those who has the same error even you are sure about the things that @zerkms mentioned :

" you need to have in the same encoding: 1) the page/form 2) table and column (if any) charset (not collation) 3) db connection. That's it. If you have all 3 of them the same utf-8 - then it should work. If it doesn't - you're missing something and need to re-check each of them "

if you are still having problem like i did, check your form process data that if there is a filter that you forgot. i had a filter for security, it was sanitizing inputs and it was only for ansi encoding, so check out everything and than it will be fine.

thanks for all who replied.

:) Somewhere in your toolchain, something is not using utf8. PHPMyAdmin is well known for this type of issue but I cannot help you much there as I much prefer the command line or scripts to play with a database. If the output is fine everywhere except in phpMyadmin I can refer you to this post that offers a lot of tips relating to phpmyadmin.

Oh, and you can specify utf-8 encoding in your instanciation call to PDO:

$con = new PDO('mysql:host=' . $server . ';dbname=' . $db . ';charset=UTF8', $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

The most basic helpful thing you can do when setting up MysqL is adding this to your /etc/my.cnf file:

[mysqld]
default-character-set=utf8
default-collation=utf8_general_ci
character-set-server=utf8
collation-server=utf8_general_ci
init-connect='SET NAMES utf8'

[client]
default-character-set=utf8

The editors can also play tricks on you. Some editors when not configured properly can switch and resave in improper encoding, causing garbled text when re-opened in utf-8. All decent IDE's and editors can be configured to handle UTF-8.

Hope this helps, good-luck.

You set the charset of the connection object to utf-8, that's good and other settings in the database should not have any impact then.

I would check that your page (the one that inserts and the one that displays) are both correctly encoded. There are two things to check: Check that your page is stored UTF-8 encoded (without BOM), this is the job of your editor/ide. Then check that you declared it correctly with something like:

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

Of course if you already have inserted data to your db from a wrong encoded page, the data in your db is invalid and cannot be displayed properly, on a correctly encoded page.

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