求助:php中进行mysql语句查询时,当字段为中文时无法查询

0 篇文章 0 订阅

求助: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);
?>

结果:
demo-1

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);
?>

结果:
demo-2

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);
?>

结果:
demo-3
4.mysql中两个表的查询结果:
demo-4
其中mysql中的字符采用的是gbk;
采用的是phpstudy的集成环境;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值