使用php mysqli操作数据库的乱码问题

    在学习PHP与MySQL时,利用PHP操作MySQL插入数据时,发现插入中文时时乱码,创建数据库以及数据库表时均制定了utf-8编码,一开始是html页面并没有设置为utf-8编码,在将html页面设置为

<meta http-equiv="content-Type" content="text/html;charset=UTF8"> 

以及PHP代码中添加

header("Content-type:text/html; charset=utf-8"); 

后,发现插入到数据库的中文依然是乱码。

    百思不得其解,后经度娘发现,需要在连接数据库后,显示设置连接使用utf-8编码。。。。

<?php
$sqldb
= new mysqli("localhost", "root", "", "project"); if (mysqli_connect_errno()) { printf("Connect failed:%s\n", mysqli_connect_error()); exit(); } mysqli_query($sqldb, "set names utf8"); //utf8 设为对应的编码 ... mysqli_close($sqldb);
?>

mysqli因为默认是latin-1编码,当你的页面时utf-8编码是,就会产生页面乱码问题。

posted on 2016-10-31 22:07  fangcl  阅读(1977)  评论(0)    收藏  举报

导航