search

Home  >  Q&A  >  body text

sql - PHP query record number problem, count exception

    mysql_select_db("zaiping", $con);
    $result = array();
    $rs = mysql_query("select count(*) as count from dept where deptName like $deptName");
    $row = mysql_fetch_array($rs);
    ChromePhp::log($row);
    $result["total"] = $row[0];

sql command line executionselect count(*) as count from dept where deptName like $deptName is no problem. It is also no problem if the number of records >=2. The problem is that when there is 1 When matching data, $result["total"]=0, why is this, why is it not 1? Thanks

大家讲道理大家讲道理2871 days ago542

reply all(3)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-16 13:18:02

    你把$row[0]改成$row[0]['count']试试;显示过滤了

    reply
    0
  • 阿神

    阿神2017-05-16 13:18:02

    $rs = mysql_query("select count(*) as count from dept where deptName like '%".$deptName."%'");
    SQL like 请加百分号

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-16 13:18:02

    When this kind of problem occurs, I usually solve it like this:

    $sql = "select count(*) as count from dept where deptName like $deptName";
    print_r($sql);        //将输出的SQL拿到MySQL去执行,看报什么错误
    $re = mysql_query($sql);
    var_dump($re);        //查看返回什么
    

    Based on the output, make corresponding judgments to see where the problem lies.

    reply
    0
  • Cancelreply