php mysqli设置编码的语法是“mysqli_query($con,'set names utf8');”,该语句表示将编码设置为“utf8”,其中“mysqli_query”函数的参数需要两个。

推荐:《PHP视频教程》
先看一段实例代码:
<?php
header ( "Content-type:text/html;charset=utf-8" ); //统一输出编码为utf-8
$con = mysqli_connect ( 'localhost', 'root', '' ); //数据库连接
if (mysqli_select_db ( $con, 'mytest' )) {
echo "数据库ok";
} else {
echo '数据库错误';
}
<span style="color:#ff0000;">mysqli_query($con,'set names utf8'); //设置读取数据后的编码</span>
$query = "select * from users";
$arr = mysqli_query ( $con, $query );
print_r ( mysqli_fetch_array ( $arr ) );
mysqli_close ( $con );
?>要点备注:
1.数据库创建时必须指定utf-8编码
立即学习“PHP免费学习笔记(深入)”;
2.创建项目后在项目上设定默认编码为utf-8
3.mysqli_query($con,'set names utf8');
注意mysqli_query和mysql_query的参数区别:mysql_query("SET NAMES 'utf8'");的参数只有一个,mysqli的需要两个,否则报错
以上就是php mysqli如何设置编码的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号