When submitting php + mysql data, we must ensure that the encoding submitted to the database is consistent with the mysql encoding so that we can save Chinese characters without question mark garbled characters, next, I will first sort out two methods to ensure consistency between mysql and the page encoding. solve the problem of garbled characters in Chinese database insertion... when submitting php + mysql data, we must ensure that the encoding submitted to the database is consistent with the mysql encoding so that we can save Chinese characters without question mark garbled characters, next, I will first sort out two methods to ensure consistency between mysql and the page encoding.
To solve the problem of garbled characters inserted into the database in Chinese, convert Chinese into UTF-8 format directly. most of the problems are caused by this problem. The code is as follows:
$str = iconv('gbk','utf-8',$str);
If you still cannot check whether the webpage code is correct, the code is as follows:
For example, because I am too lazy to write, this example on the internet is quite clear. the code for creating a table is as follows:
Create TABLE `net_city` ( `cityid` smallint(4) NOT NULL auto_increment, `cityname` varchar(80) NOT NULL default '', `provinceid` smallint(2) NOT NULL default '0', `inarea` varchar(5000) NOT NULL default '', `outarea` varchar(5000) NOT NULL default '', `tel` varchar(400) NOT NULL default '', PRIMARY KEY (`cityid`)
) ENGINE = MyISAM default charset = utf8;
PHP code:
$ Conn = mysql_connect ("localhost", "username", "password"); mysql_query ("set names 'utf8'", $ conn); mysql_select_db ("www.phprm.com data name ", $ conn); $ exec = "insert into net_city (cityname, inarea, outarea, tel) values ('". $ link_cityname. "','". $ link_inarea. "','". $ link_outarea. "','". $ link_tel. "')"; $ result = mysql_query ($ exec, $ conn); if ($ result) {echo "1" ;}else {echo "0 ";} mysql_close ($ conn );
Later, I tried to use all of them as gbk, which is also acceptable. when querying data, we directly use mysql_query () to SET: mysql_query ("SET NAMES GBK "); // Set the GBK encoding.
For example, the code for the "insert. php" page is as follows:
Tutorial link:
Reprint at will ~ However, please keep the tutorial address★