Home > Article > Backend Development > How to set encoding in php mysqli
php The syntax for setting the encoding of mysqli is "mysqli_query($con,'set names utf8');". This statement means to set the encoding to "utf8", in which the "mysqli_query" function requires two parameters.
Recommendation: "PHP Video Tutorial"
Let’s look at an example code first:
<?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 ); ?>
Important notes:
1. UTF-8 encoding must be specified when creating the database
2. After creating the project, set the default encoding to utf-8
3. mysqli_query($con,'set names utf8');
Note the parameter difference between mysqli_query and mysql_query: mysql_query("SET NAMES 'utf8'"); has only one parameter, mysqli's Two are required, otherwise an error will be reported
The above is the detailed content of How to set encoding in php mysqli. For more information, please follow other related articles on the PHP Chinese website!