aCarella
aCarella

Reputation: 2578

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?

Upvotes: 0

Views: 359

Answers (3)

user3419778
user3419778

Reputation: 866

Please change the code from

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

}

To

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

}

Upvotes: 1

TimoStaudinger
TimoStaudinger

Reputation: 42530

Adding to someOne's solution, var_dump() does indeed give you the values of your array as well, in addition to the component names. The numbers or strings in brackets after the data type are the values of your array:

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

Here, the field current_field has the type int and the value 0. num_rows also has the type int and the value 88 etc.

Upvotes: 1

someOne
someOne

Reputation: 1675

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

Upvotes: 3

Related Questions