Home > Backend Development > PHP Problem > What should I do if php sends garbled strings to the mysql database?

What should I do if php sends garbled strings to the mysql database?

藏色散人
Release: 2023-03-17 22:32:01
Original
3796 people have browsed it

Solution for php to pass garbled strings to the mysql database: 1. Check the database encoding method through "show variables like 'character%';"; 2. Set it through "set character_set_server = 'utf8';" Just encode it as utf8.

What should I do if php sends garbled strings to the mysql database?

The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer

php passes characters into the mysql database What to do if a string of garbled characters appears?

PHP inserts Chinese into the mysql database and displays garbled characters

The garbled characters are as follows:

What should I do if php sends garbled strings to the mysql database?

##Solution

View Database encoding method:

//查询
show variables like 'character%';
//设置
set character_set_server = 'utf8';
Copy after login

What should I do if php sends garbled strings to the mysql database?

Set HTML page encoding

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Copy after login

PHP file encoding setting

//消息头部
header("Content-Type:text/html; charset=utf-8");
//数据库连接方式
$conn = new mysqli($servername,$username,$password,$dbname);
mysqli_set_charset($conn,&#39;UTF8&#39;);
//注意,下面这种方式不行!!!
$conn = new mysqli($servername,$username,$password,$dbname);
mysqli_query(“set names utf8”);
Copy after login

Related introduction:

UTF-8 (8-bit, Universal Character Set/Unicode Transformation Format) is a variable-length character encoding for Unicode. It can be used to represent any character in the Unicode standard, and the first byte in its encoding is still compatible with ASCII, so that the original software that processes ASCII characters can continue to be used without or with only a few modifications. Therefore, it has gradually become the preferred encoding for email, web pages, and other applications that store or transmit text.

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of What should I do if php sends garbled strings to the mysql database?. 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