Trichophyton
Trichophyton

Reputation: 645

fetch_array() - cannot add multiple array fields to new array

I am trying to return a json from a MySQL query to use it with xCode but I cannot get an array of several objects with multiple fields. I have read the documentation on php.net and over here, but I still can't get it.

1) Let's say I have a MySQL table with 3 rows (for example 3 people). Each row contains 3 fields (lastname, firstname, dateOfBirth):

$result = mysqli_query($mysqli, $sql) // where $sql = "SELECT * FROM tbl_syncList"

$resultArray = array();

while ($row = $result->fetch_array()) {
    $resultArray[] = $row["lastname"];
}
echo json_encode($resultArray); // --> return "["Lastname1", "Lastname2"...] - that's okay, I understand I return the value of the key "lastname"

I don't know how to get an array with the entire rows (all the fields at once), like:

[["Firstname1", "Lastname1", "dateOfBirth1"], ["Firstname2", "Lastname2", "dateOfBirth2"],...]

I tried to replace

$resultArray[] = $row["lastname"];

with:

$resultArray[] = $row;

but it just gives the world...

"array"

...with no content. Does anyone have an idea?

Thanks a lot!

Upvotes: 0

Views: 54

Answers (1)

Markus AO
Markus AO

Reputation: 4889

Are you sure the array is properly populated to begin with? print_r($resultArray) and see what you have there with = $row (which is correct).

Upvotes: 1

Related Questions