0

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!

1
  • 3
    $resultArray[] = $row; is correct. You can't echo $row but it will give the json you want. Commented Mar 4, 2015 at 22:30

1 Answer 1

1

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

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

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.