用php将中文写入mysql,在phpmyadmin中查看显示为乱码,求解决方法!!
如果在mysql命令行界面插入中文在phpmyadmin中就显示正常,<?phpheader("Content-Type:text/html;Charset=UTF-8"...
如果在mysql命令行界面插入中文在phpmyadmin中就显示正常,
<?php
header("Content-Type:text/html;Charset=UTF-8");
$keyword="20112897+网络无法连接";
$token = strtok($keyword, "+");
$i=0;
while ($token != false)
{
$records[$i]=$token;
$token = strtok("+");
$i++;
}
$mysqli = mysqli_connect("localhost","root","","test");
if(mysqli_connect_errno()){
printf("Connect failed: %s\n",mysqli_connect_error());
exit();
}else{
mysql_query("set names utf8");
$sql="INSERT INTO nametable (number,name)VALUES('$records[0]','$records[1]')";
//mysql_query("set names utf8");
$res=mysqli_query($mysqli,$sql);
//mysql_query("SET NAMES UTF8");
if($res===TRUE){
echo "A record has been inserted.";
}else{
printf("Could not insert record :%s\n",mysqli_error($mysqli));
}
mysqli_close($mysqli);
}
?>
我用的是xampp集成环境,这是我的代码
如果在用php读出来 之前用php写进去的显示正常 而在mysql命令行添加的显示为??? 展开
<?php
header("Content-Type:text/html;Charset=UTF-8");
$keyword="20112897+网络无法连接";
$token = strtok($keyword, "+");
$i=0;
while ($token != false)
{
$records[$i]=$token;
$token = strtok("+");
$i++;
}
$mysqli = mysqli_connect("localhost","root","","test");
if(mysqli_connect_errno()){
printf("Connect failed: %s\n",mysqli_connect_error());
exit();
}else{
mysql_query("set names utf8");
$sql="INSERT INTO nametable (number,name)VALUES('$records[0]','$records[1]')";
//mysql_query("set names utf8");
$res=mysqli_query($mysqli,$sql);
//mysql_query("SET NAMES UTF8");
if($res===TRUE){
echo "A record has been inserted.";
}else{
printf("Could not insert record :%s\n",mysqli_error($mysqli));
}
mysqli_close($mysqli);
}
?>
我用的是xampp集成环境,这是我的代码
如果在用php读出来 之前用php写进去的显示正常 而在mysql命令行添加的显示为??? 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询