求助:php中进行mysql语句查询时,当字段为中文时无法查询
1.当查询条件为英文时,可以正常查询并返回结果;
<?php
$con = mysql_connect('localhost','root','root');
if($con){
echo("ok!"."<br>");
}else{
echo("error!"."<br>");
}
$selectDb = mysql_select_db('zp',$con);
if($selectDb){
echo("ok!"."<br>");
}else{
echo("error!"."<br>");
}
$msg = "select * from user where name = 'tom'";
$result = mysql_query($msg,$con);
$num = mysql_num_rows($result);
echo($num."<br>");
$resultArr = mysql_fetch_array($result);
var_dump($resultArr);
?>
结果:
2.当改成中文时返回0,无法查询出结果;
<?php
$con = mysql_connect('localhost','root','root');
if($con){
echo("ok!"."<br>");
}else{
echo("error!"."<br>");
}
$selectDb = mysql_select_db('zp',$con);
if($selectDb){
echo("ok!"."<br>");
}else{
echo("error!"."<br>");
}
$msg = "select * from user where name = '张三'";
$result = mysql_query($msg,$con);
$num = mysql_num_rows($result);
echo($num."<br>");
$resultArr = mysql_fetch_array($result);
var_dump($resultArr);
?>
结果:
3.另一个表是以中文命名,直接报错
<?php
$con = mysql_connect('localhost','root','root');
if($con){
echo("ok!"."<br>");
}else{
echo("error!"."<br>");
}
$selectDb = mysql_select_db('zp',$con);
if($selectDb){
echo("ok!"."<br>");
}else{
echo("error!"."<br>");
}
$msg = "select * from user1 where 用户名 = 'tom'";
$result = mysql_query($msg,$con);
$num = mysql_num_rows($result);
echo($num."<br>");
$resultArr = mysql_fetch_array($result);
var_dump($resultArr);
?>
结果:
4.mysql中两个表的查询结果:
其中mysql中的字符采用的是gbk;
采用的是phpstudy的集成环境;