LEN

使用mysqli 之后的 php中文乱码问题要点
先看一段实例代码:<?php header ( "Content-type:text/...
扫描右侧二维码阅读全文
11
2016/11

使用mysqli 之后的 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 '数据库错误';  
    }  

    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的需要两个,否则报错

Last modification:February 9th, 2017 at 05:24 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment