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

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

WBOY
Release: 2016-06-06 20:26:19
Original
1288 people have browsed it

<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>
Copy after login
Copy after login

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

<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>
Copy after login
Copy after login

我检查了数据库,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>
Copy after login
Copy after login

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

<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>
Copy after login
Copy after login

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

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

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template