Home >Backend Development >PHP Tutorial >TP framework and PDO character encoding issues
If you use a frame to insert Chinese and view it, it will not be garbled. If you use PDO to insert and query, the Chinese will not be garbled. But if you use a frame to view the Chinese inserted by PDO, it will be garbled. If you use PDO output to view the Chinese inserted by TP, it will also be garbled. Why is this? If you insert Chinese directly into the mysql black window, the Chinese will not be garbled when using the frame, but it will be garbled when using PDO. I added this sentence to PDO and it does not work. header('content-type: text/html; charset=utf-8; '); 前 I have changed the character codes before. After that, I checked the character code of the table and found why the field is the UFT8 format table but the GBK format? In a situation like this, how can the Chinese queried by the framework and PDO methods not be garbled? Is there any relationship between the encoding of a certain field and the encoding of this table? Which one should prevail?
This is the current character encoding of mysql
<code> CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `age` int(15) NOT NULL, `class` varchar(400) CHARACTER SET utf8 DEFAULT NULL, `name` varchar(12) CHARACTER SET utf8 DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=390 DEFAULT CHARSET=gbk | </code>Reply content:
This is the current character encoding of mysql
<code> CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `age` int(15) NOT NULL, `class` varchar(400) CHARACTER SET utf8 DEFAULT NULL, `name` varchar(12) CHARACTER SET utf8 DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=390 DEFAULT CHARSET=gbk | </code>The tp framework uses utf8 format, so you must have a problem with the database character set. You need to set the database to utf8, and the table also needs to be set to utf8.
Look at the SQL code above, the field is utf8, But the watch is gbk, there must be something wrong
<code> CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `age` int(15) NOT NULL, `class` varchar(400) CHARACTER SET utf8 DEFAULT NULL, `name` varchar(12) CHARACTER SET utf8 DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=390 DEFAULT CHARSET=utf8 |</code>
<code>ALTER DATABASE 数据库名 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci</code>