Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of php insert mysql garbled code

Shulou Source: shulou.com Published: 2022-06-02 05:18:25 10月18日 Update

This article introduces the relevant knowledge of "how to solve the php insert mysql garbled problem". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Php insert mysql garbled solutions: 1, save the php page to UTF-8 encoding format; 2, use UTF8 format for data submission; 3, set "ENGINE=MyISAM DEFAULT CHARSET=utf8;" in the database.

This article operating environment: Windows7 system, PHP7.1 version, Dell G3 computer.

How to solve the php insert mysql garbled problem?

Solution to the problem of Chinese garbled code when php is inserted into mysql database

Today, I reinstalled the system and reinstalled the running environment of PHP, but I didn't pay attention to it too much. I want to write something to 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 code, so I stipulated in the web page that the language was found as gbk. I looked it up for a long time and made a summary:

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

2.php uses UTF8 for data submission.

3.MYSQL uses ENGINE=MyISAM DEFAULT CHARSET=utf8 when creating tables

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

Build a table:

Create TABLE `city` (`cityid` smallint (4) NOT NULL auto_increment, `cityname` varchar (80) NOT NULL default'', `provinceid` smallint (2) NOT NULL default '0mm, `inarea`varchar (5000) NOT NULL default'', `outarea`varchar (5000) NOT NULL default'', `tel`city` NOT NULL default'', PRIMARY KEY (`cityid`) ENGINE=MyISAM DEFAULT CHARSET=utf8

The code in PHP:

$conn=mysql_connect ("localhost", "user name", "password"); mysql_query ("set names' utf8'", $conn); mysql_select_db ("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); "how to solve the php insert mysql garbled problem" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Data garbled problem format content database time more environment knowledge system coding page UTF-8 Chinese review no practical clear successful learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker OPPO Reno vpn Huawei