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

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

WBOY
WBOYOriginal
2016-06-06 20:26:191209browse

<code>        $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;</code>

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

<code>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"
  }
}</code>

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

回复内容:

<code>        $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;</code>

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

<code>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"
  }
}</code>

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

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn