当前位置:Gxlcms > PHP教程 > PHP使用fetch_assoc()获取查询结果时数据类型都变了

PHP使用fetch_assoc()获取查询结果时数据类型都变了

时间:2021-07-01 10:21:17 帮助过:102人阅读

        $res=$this->conn->query($sql) or die('execute sql error:'.$this->conn->error);
        $arr=array();
        $i=0;
        while ($row=$res->fetch_assoc()){
            $arr[$i]=$row;
            $i++;
        }
        $res->free();
        $this->conn->close();
        return $arr;

上面这一段是查询的,结果都拿到了,可是当我打印结果数组是,发现数据全都变成了字符串

array(3) {
  [0] => array(3) {
    ["id"] => string(1) "1"
    ["name"] => string(8) "zhangsan"
    ["age"] => string(2) "23"
  }
  [1] => array(3) {
    ["id"] => string(1) "2"
    ["name"] => string(4) "lisi"
    ["age"] => string(2) "18"
  }
  [2] => array(3) {
    ["id"] => string(1) "3"
    ["name"] => string(6) "wangwu"
    ["age"] => string(2) "20"
  }
}

我检查了数据库,id和age这两个字段确实是int类型没错,可是为什么数据查出来后类型都变了?

回复内容:

        $res=$this->conn->query($sql) or die('execute sql error:'.$this->conn->error);
        $arr=array();
        $i=0;
        while ($row=$res->fetch_assoc()){
            $arr[$i]=$row;
            $i++;
        }
        $res->free();
        $this->conn->close();
        return $arr;

上面这一段是查询的,结果都拿到了,可是当我打印结果数组是,发现数据全都变成了字符串

array(3) {
  [0] => array(3) {
    ["id"] => string(1) "1"
    ["name"] => string(8) "zhangsan"
    ["age"] => string(2) "23"
  }
  [1] => array(3) {
    ["id"] => string(1) "2"
    ["name"] => string(4) "lisi"
    ["age"] => string(2) "18"
  }
  [2] => array(3) {
    ["id"] => string(1) "3"
    ["name"] => string(6) "wangwu"
    ["age"] => string(2) "20"
  }
}

我检查了数据库,id和age这两个字段确实是int类型没错,可是为什么数据查出来后类型都变了?

类似问题参考
http://segmentfault.com/q/1010000002957162

人气教程排行