无法将MySQL查询结果导入PHP数组

时间:2015-05-17 03:44:08

标签: php mysql arrays var-dump

我正在尝试将mysql查询结果放入PHP数组中。当我运行我的脚本并打印数组时,它不会显示从mysql表中获取的信息,而是显示有关该数组的信息。

这是我的代码:

<?php
class db {
  public $conn;
  function dbLogin() {
    $this->conn = new mysqli("1.2.4.3","user","pwd","database");    
  }

  public function selectQuery() {
    $this->dbLogin();
    $query = "
        SELECT      *
        FROM        myTable
    ";
    echo "<pre>";
    $resultArray = Array();
    if ($result = $this->conn->query($query)) {
        while ($row = $result->fetch_array()) {
            $resultArray[] = $result;
        }

    }
    var_dump($resultArray);
    echo "</pre>";
  }
}

$fData = new db();
$fData->selectQuery();
?>

当我运行上面的脚本时,我得到了这个:

array(88) {
[0]=>
object(mysqli_result)#3 (5) {
["current_field"]=>
int(0)
["field_count"]=>
int(34)
["lengths"]=>
NULL
["num_rows"]=>
int(88)
["type"]=>
int(0)
}
[1]=>
object(mysqli_result)#3 (5) {
["current_field"]=>
int(0)
["field_count"]=>
int(34)
["lengths"]=>
NULL
["num_rows"]=>
int(88)
["type"]=>
int(0)
}
...
}

以上是页面上显示的88个显示/对象/数组实例中的前两个。但它不会显示来自数据库的实际信息。

到目前为止我已尝试过:

我已经尝试了echovar_dumpprint_rprintf数组而没有运气。我真的很想在数组中看到查询结果,所以我知道我的脚本按照我想要的方式工作,但我似乎无法弄清楚如何做到这一点。

我知道该查询工作正常,如果我只用$resultArray[] = $result;替换echo $result[0] . $result[1] (etc...),我可以看到该表中的结果。

如何从查询中查看数组中的数据,以便知道我的脚本正在运行?

3 个答案:

答案 0 :(得分:3)

$resultArray[] = $result;更改为$resultArray[] = $row;

答案 1 :(得分:1)

除了组件名称之外,添加到someOne的解决方案,var_dump()确实也为您提供了数组的值。数据类型后括号中的数字或字符串是数组的值:

array(88) {
[0]=>
object(mysqli_result)#3 (5) {
["current_field"]=>
int(0)
["field_count"]=>
int(34)
["lengths"]=>
NULL
["num_rows"]=>
int(88)
["type"]=>
int(0)
}

此处,字段current_field的类型为int,值为0num_rows也有类型int和值88等。

答案 2 :(得分:1)

请更改

中的代码
if ($result = $this->conn->query($query)) {
    while ($row = $result->fetch_array()) {
        $resultArray[] = $result;// this would be $row
    }

}

if ($result = $this->conn->query($query)) {
    while ($row = $result->fetch_array()) {
        $resultArray[] = $row;
    }

}
相关问题