关于thinkphp数据库无法插入中文的问题

浏览:1734 发布日期:2016/07/31 分类:求助交流 关键字: BUG 数据库 无法插入中文
我用thinkphp不能插入中文(查看数据库是空白),但可以插入英文。我用原生PHP就可以插入中文,这是为什么?我的数据也试过用iconv进行了gb2312转utf-8但仍无法插入到数据库中,这是为什么??
class IndexAction extends Action {
public function index(){
$this->display("index2");
}
public function show(){
if(empty($_POST))
{
echo iconv("utf-8","gb2312","页面不存在");
header("refresh:2;url=".u("index"));
}
else
{
//原生PHP插入
$conn=mysql_connect("localhost","root","");
mysql_select_db("think_post",$conn);
mysql_query("set names gb2312",$conn);
mysql_query("insert into `think_user`(`id`,`nicheng`,`liuyan`) values('','{$_POST['nicheng']}','{$_POST['liuyan']}')",$conn)or die(mysql_error());


//thinkphp框架插入
$arr1=array("nicheng"=>i('nicheng'),"liuyan"=>i('liuyan'));
$arr=array();
foreach($arr1 as $key=>$val)
{
$arr[]=iconv("gb2312","utf-8",$val);
}
m("user")->data($arr)->add();
}
}
}


/*
//这是创建数据库的sql代码
$conn=mysql_connect("localhost","root","");
mysql_query("set names gb2312",$conn)or die(mysql_error()."创建字体失败");
if(!mysql_select_db("think_post",$conn))
{
mysql_query("create database `think_post`",$conn)or die(mysql_error()."创建数据库失败");
mysql_select_db("think_post",$conn);
mysql_query("create table `think_user`(`id` int(2) unsigned not null auto_increment primary key,`nicheng` char(20) character set utf8 collate utf8_unicode_ci not null,`liuyan` char(30) character set utf8 collate utf8_unicode_ci not null)")or die(mysql_error()."创建表失败");
}
*/
最佳答案
评论( 相关
后面还有条评论,点击查看>>