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.
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:
##SolutionView Database encoding method://查询 show variables like 'character%'; //设置 set character_set_server = 'utf8';
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
//消息头部 header("Content-Type:text/html; charset=utf-8"); //数据库连接方式 $conn = new mysqli($servername,$username,$password,$dbname); mysqli_set_charset($conn,'UTF8'); //注意,下面这种方式不行!!! $conn = new mysqli($servername,$username,$password,$dbname); mysqli_query(“set names utf8”);
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!