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?

藏色散人
藏色散人Original
2023-03-01 09:28:473758browse

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

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" />

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

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!

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