How to solve php insert mysql garbled problem

藏色散人
Release: 2023-03-14 15:38:01
Original
2648 people have browsed it

php insert mysql garbled solution: 1. Save the php page as UTF-8 encoding format; 2. Submit data in UTF8 format; 3. Set "ENGINE=MyISAM DEFAULT CHARSET=" in the database utf8;".

How to solve php insert mysql garbled problem

The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

How to solve the php insert mysql garbled problem?

Solution to the problem of Chinese garbled characters inserted into mysql database by php

Today I reinstalled the system and reinstalled the PHP operating environment. I didn’t pay too much attention to it. I thought Write something to review and review... When I used INSERT INTO to add data to the data table, I found that as long as the data was in Chinese, it was displayed in garbled characters, so I specified the language in the web page as gbk and found that it still didn't work. On the Internet After searching for a long time, I made a summary:

1.php page should be saved in UTF-8 encoding format.

2.php must use UTF8 when submitting data.

3. When creating a table in MYSQL, use ENGINE=MyISAM DEFAULT CHARSET=utf8;

Example (because I am too lazy to write, this example on the Internet feels quite clear):

Build table:

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;
Copy after login

Code in PHP:

$conn=mysql_connect("localhost", "用户名", "密码");
mysql_query("set names 'utf8'",$conn);
mysql_select_db("数据名",$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);
Copy after login

Later I tried to use gbk for all, and it was ok~

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to solve php insert mysql garbled problem. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template