search
HomeBackend DevelopmentPHP ProblemHow to solve php insert mysql garbled problem

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;

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);

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),