How to set encoding in php mysqli

藏色散人
Release: 2023-03-04 17:40:01
Original
4638 people have browsed it

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.

How to set encoding in php mysqli

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 ( &#39;localhost&#39;, &#39;root&#39;, &#39;&#39; ); //数据库连接
if (mysqli_select_db ( $con, &#39;mytest&#39; )) {
echo "数据库ok";
} else {
echo &#39;数据库错误&#39;;
}
 
<span style="color:#ff0000;">mysqli_query($con,&#39;set names utf8&#39;); //设置读取数据后的编码</span>
$query = "select * from users";
 
$arr = mysqli_query ( $con, $query );
print_r ( mysqli_fetch_array ( $arr ) );
mysqli_close ( $con );
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template