0

I am trying to return an array called from a public function stored in a PHP file. The returned array I need to export is in JSON format. I have the below code for calling the array (which in other cases works), but the output is just Array (the sql in phpMyAdmin returns all data)

This is the public function that should return the array, stored in a general PHP class file.

public function getIssueList() {
    $sql = "select * from IssueData";
    $returnValue = array();

    $result = $this->conn->query($sql); // makes the connection and executes the sql

    if ($result != null) {
        $row = $result->fetch_array(MYSQLI_ASSOC);
        if (!empty($row)) {
            $returnValue = $row;
        }
    }

    return $returnValue;

}

Then I call the public function from below code:

$result = $dao->getIssueList(); //opens the connection and calls the public function

echo $result;

But the echo result I get is just the word "Array"

Above code works for other public functions, but it returns only one row and not multiple as I need in this case. Also I need to get the array as associative.

What might be wrong?

2
  • change $returnValue = $row; to $returnValue[] = $row; and echo $result; to print_r($result);. Commented Nov 15, 2015 at 20:49
  • (1) ->fecth_array() -> Fetch a result row..., so if you want mulitiple rows you need to do a loop, and add each row to an array. (2) since return $returnValue; will return an array, you can't just echo $result;. You could use print_r($result); or var_dump($result) or ... Commented Nov 15, 2015 at 20:49

4 Answers 4

1

You can json_encode (http://php.net/manual/en/function.json-encode.php)

echo json_encode($result);
Sign up to request clarification or add additional context in comments.

Comments

0

You will have to return $result as json_encode($result), also, to fill your array correctly you have to do $returnValue[] = $row;

(You might want to do a while loop to fill your array)

Comments

0

To get multiple rows rather than just one, use a while loop:

if ($result != null) {
$row = $result->fetch_array(MYSQLI_ASSOC);
    while(!empy($row)){
        $returnValue = array_merge($returnValue, $row);
        $row = $result->fetch_array(MYSQLI_ASSOC);
    }
return $returnValue; 
}

1 Comment

why not just do while($row = $result->fetch_array(MYSQLI_ASSOC)){ $returnValue = array_merge($returnValue, $row);} instead of $row = $result->fetch_array(MYSQLI_ASSOC); while(!empy($row)){ $returnValue = array_merge($returnValue, $row); $row = $result->fetch_array(MYSQLI_ASSOC); }, as that is a lot of redundant code.
0

ok mostly the problem was that I was using echo instead of print_r.

Also changed below code:

if ($result != null) {
    $row = $result->fetch_array(MYSQLI_ASSOC);
    if (!empty($row)) {
        $returnValue = $row;
    }
}

to:

if ($result != null) {
        while ($returnValue[] = $result->fetch_array(MYSQLI_ASSOC));
}

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.