1

I'm trying to make a json output from my db in php with all of users from sql table but i don't know how to do that..i'm new with this. I want the code to output like this.

[
    {
        "name": "Maybell",
        "avatar": "zeldman/128.jpg",
        "data": {
            "company": "Alibaba",
            "title": "Engineer"
        }
    }
]

but the my code does the following output and is not ok.

{
    "name": "Maybell",
    "avatar": "zeldman\/128.jpg",
    "company": "alibaba"
    "title": "Engineer"

}

Here is the Php code:

<?php
header("Content-type: text/json");
$db = mysqli_connect("localhost","root","","testest");
//MSG
$query = "SELECT * FROM mls_users LIMIT 20";
$result = mysqli_query($db, $query);
//Add all records to an array
$rows = array();
while($row = $result->fetch_array()){
    $name = $row['name'];
    $avatar = $row['avatar'];
    $company= $row['company'];
    $title= $row['title'];
}
//Return result to jTable
$qryResult = array();
$qryResult['name'] = $name;
$qryResult['avatar'] = $avatar;
$qryResult['company'] = $company;
$qryResult['title'] = $title;

 echo json_encode($qryResult,JSON_PRETTY_PRINT);
mysqli_close($db);

?>

4 Answers 4

4

Just change:

//Add all records to an array
$rows = array();
while($row = $result->fetch_array()){
    $name = $row['name'];
    $avatar = $row['avatar'];
    $company= $row['company'];
    $title= $row['title'];
}
//Return result to jTable
$qryResult = array();
$qryResult['name'] = $name;
$qryResult['avatar'] = $avatar;
$qryResult['company'] = $company;
$qryResult['title'] = $title;

To:

//Add all records to an array
$qryResult = [];
while ($row = $result->fetch_array()) {
    $qryResult[] = [
        'name'   => $row['name'],
        'avatar' => $row['avatar'],
        'data'   => [
            'company' => $row['company'],
            'title'   => $row['title'],
        ],
    ];
}

You can skip setting the intermediate variables and just add onto the $qryResult array directly.

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

5 Comments

No problem @Alex :). If my answer helped you, don't forget to accept it.
Not sure why you're getting downvoted, this is correct.
Won't this still only output the last row? Because the $qryResult array is being initialized after the loop and only the last state of the variables being added to it.
take a mix of this answer and mine. i did correct the fetching of all rows but oversaw the data array. edit: i edited my answer, so it has the data array too
@Connum Yes, you're right, I've updated my answer. Thanks for your feedback :)
2

try this:

<?php
header("Content-type: text/json");
$db = mysqli_connect("localhost","root","","testest");
//MSG
$query = "SELECT * FROM mls_users LIMIT 20";
$result = mysqli_query($db, $query);
//Add all records to an array
$users = array();
while($row = $result->mysqli_fetch_assoc()){
    $users[] = [
        'name' => $row['name'],
        'avatar' => $row['avatar'],
        'data' => [
            'company' => $row['company'],
            'title' => $row['title']
        ]
    ]
}

echo json_encode($users,JSON_PRETTY_PRINT);
mysqli_close($db);

?>

I changed fetch_row() to mysqli_fetch_assoc() and then actually made an array with all fetched rows. You kind of wanted to do that i can see that in comments but you didnt. Then just json encode it.

The outlining brackets [ and ] should come automatically when the array has multiple elements in it.

1 Comment

Thank you guys! u're the best :)
0

You are almost there, you just need to wrap it in an array and make data an array.

$qryResult = array();
$qryResult['name'] = $name;
$qryResult['avatar'] = $avatar;
// Now make an array to hold data
$data = array();
$data['company'] = $company;
$data['title'] = $title;
// Add data to qryResult
$qryResult['data'] = $data
// Wrap qryResult in array so output will be wrapped in array
$outPutResults = array($qryResult);
echo json_encode($outPutResults,JSON_PRETTY_PRINT);

Comments

0
$qryResult = array();
$qryResult['name'] = $name;
$qryResult['avatar'] = $avatar;
$qryResult['data']['company'] = $company;
$qryResult['data']['title'] = $title;

Try that way. You are asking for a multidimensional-array so you need to set it up as one.

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.