How to solve the garbled character string passed from php to the mysql database

03-22-2023

Today, the editor will share with you the relevant knowledge points on how to solve the problem of garbled strings sent from php to the mysql database. For your reference, I hope you will gain something after reading this article. Let's take a look at it together.


The solution to the garbled character string passed from php to the mysql database: 1. Check the database encoding method through show variables like 'character%'; 2. Through set character_set_server = 'utf8'; Just set the encoding to utf8.

The garbled characters are as follows:


Solution

Check the database encoding method:

//query show variables like 'character%'; //set up set character_set_server = 'utf8';

1.jpg

Set HTML page encoding



PHP file encoding settings


//Message 

header header("Content-Type:text/html; charset=utf-8"); 

//Database connection method 

$conn = new mysqli($servername,$username,$password,$dbname); 

mysqli_set_charset($conn,'UTF8'); 

//Note, the following method does not work! ! ! 

$conn = new mysqli($servername,$username,$password,$dbname); 

mysqli_query(set names utf8);


Related introduction:

UTF-8 (8 bits, Universal Character Set/Unicode Transformation Format) is for A variable-length character encoding of Unicode. It can be used to represent the Unicode standardAny character, and the first byte in its encoding is still compatible with ASCII, so that the software that originally processed ASCII characters can continue to be used without or with only a small amount of modification. Therefore, it has gradually become the preferred encoding for email, web pages, and other applications that store or transmit text.


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us