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编码
2.创建项目后在项目上设定默认编码为utf-8
3.mysqli_query($con,'set names utf8');
注意mysqli_query和mysql_query的参数区别:mysql_query("SET NAMES 'utf8'");的参数只有一个,mysqli的需要两个,否则报错