在PHP数组中获取MySQL查询结果有问题


Having Trouble Getting a MySQL Query Result into a PHP Array

我试图将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个display/object/array实例中的前两个。但是它不显示来自数据库的实际信息。

我已经试过了:

我已经尝试了echo, var_dump, print_rprintf的数组没有运气。我真的很想在数组中看到查询结果,这样我就知道我的脚本是按照我想要的方式工作的,但我似乎不知道如何做到这一点。

我知道一个事实,查询工作得很好,如果我只是用echo $result[0] . $result[1] (etc...)替换$resultArray[] = $result;,我可以看到从表的结果。

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

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

添加到某人的解决方案,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等。

请从

更改代码
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;
    }
}