Home How can I prettify Join Result on Rest API response object?
Reply: 1

How can I prettify Join Result on Rest API response object?

Stefan Lazarević
1#
Stefan Lazarević Published in 2017-12-08 00:42:46Z

I started learning about building REST API using PHP as core language and I came into issue with creating response object. I have table users and user_social inside my MySQL database. What I am trying to achieve is to get response object to be like this:

{
    id,
    name,
    age,
    social: [
        twitter,
        facebook,
        github
    ]
}

My sql query looks like this currently:

select users.id, users.name, users.age, user_social.* from users inner join user_social on users.id = user_social.user_id limit 10;

And my JSON response looks like this currently:

{
    id,
    name,
    age,
    twitter,
    facebook,
    github
}

So the question is, how can I group results from join to be in structure like I want to be? Is there MySQL way or I should map it by looping through each record inside PHP and then send response.

Evert
2#
Evert Reply to 2017-12-11 00:04:12Z

MySQL just returns rows and fields. You should reformat it with PHP.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.716536 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO