0

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?

1
  • $result is your query. Try dumping $row Commented May 17, 2015 at 3:48

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Silly oversight on my part. This did it. Thanks!
1

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.

Comments

1

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.