Search code examples
phpmysqlarraysvar-dump

Having Trouble Getting a MySQL Query Result into a PHP Array


I am trying to get a mysql query result into a PHP array. When I run my script and print the array, it does not display the information that it gets from the mysql table and instead just displays information about the array.

Here is my code:

<?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();
?>

When I run the above script, I get this:

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)
}
...
}

The above are the first two of 88 instances of the display/object/array of what is shown on the page. But it does not display the actual information coming from the database.

What I've tried so far:

I've tried to echo, var_dump, print_r, and printf the array with no luck. I'd really like to see the query result in the array so I know that my script is working the way I want it to, but I can't seem to figure out how to do that.

I know for a fact that that query works fine, and that if I just replace $resultArray[] = $result; with echo $result[0] . $result[1] (etc...), I can see the results from that table.

How do I view the data in the array from my query so I know that my script is working?


Solution

  • Change $resultArray[] = $result; to $resultArray[] = $row;.