Business scenario: The php page calls the mysql stored procedure, which has 1 input parameter and 1 output parameter.
Problem: It can be executed normally, but garbled characters are always displayed after entering the parameters into the database.
PHP page code is as follows
<head> <meta charset="utf-8"> </head> <?php $conn = new MySQLi("数据库地址","数据库用户","密码","数据库名"); mysqli_query($conn,"SET NAMES utf8"); $info_name_cn='测试x201'; $info_name_cn=mb_convert_encoding($info_name_cn,'UTF-8'); $result=$conn->query("CALL x2('$info_name_cn',@exeout_rows)"); $result=$conn->query("SELECT @exeout_rows"); $recordset=mysqli_fetch_assoc($result); $exeout_rows=(int)$recordset["@exeout_rows"]; ?> <div>---|<?php echo $exeout_rows; ?>|<?php echo mb_detect_encoding($exeout_rows); ?>|---</div>
mysql stored procedure code is as follows
CREATE DEFINER=`数据库名`@`%` PROCEDURE `x2`( IN exein_info_name_cn VARCHAR(5) ,OUT exeout_rows int ) BEGIN set exeout_rows=1; insert into 测试表 ( info_name_cn ) values ( exein_info_name_cn ); END
In mysql database, the option of the test table is "utf8/ utf8_ganaral_ci", the character set and sorting order of the internal field info_name_cn in the test table are "utf8/utf8_ganaral_ci"
I tried different conversions to different encoding methods - the following text
but the results were all garbled - the results after execution The following picture
(id is)29: No conversion
//$info_name_cn=mb_convert_encoding($info_name_cn,'UTF-8');
(id is)30:Convert to UTF-8
$info_name_cn=mb_convert_encoding($info_name_cn,'UTF-8');
(id is)31: Convert to GB2312
$info_name_cn=mb_convert_encoding($info_name_cn,'GB2312' );
(id is)32:convert to GBK
$info_name_cn=mb_convert_encoding($info_name_cn,'GBK');
(id is)33:convert to BIG5
$info_name_cn=mb_convert_encoding($info_name_cn,'BIG5');
(id is)34: Convert to ASCII
$info_name_cn=mb_convert_encoding($info_name_cn,'ASCII');
In addition, run the following code to add Chinese to the database normally
<?php $link = @mysql_connect("数据库地址","数据库用户名","数据库密码") or die("连接失败" .mysql_error()); @mysql_select_db("数据表") or die("连接失败".mysql_error); function insert(){ mysql_query("set names utf8"); $sqlinsert = "insert into 测试表(info_name_cn) values('李四')"; $resultinsert = mysql_query($sqlinsert); if($resultinsert){ echo "insert data success"; }else{ echo "insert data fail".mysql_error(); } } insert(); mysql_close($link); ?>
*.php file encoding problem
Supplement: In the same situation, I changed the file saving encoding method to ASCI, and the page code ran normally after setting it to GBK.