getting JSON data-tree from MySQL
I have a newsfeed similar to Facebook's newsfeed.
It means that I need to get not "straight" rows & cols datasets from MySQL but more like "trees" of data with different length of branches (and different contents).
If I go straight I will end up with lots of nested cycles in PHP - because each type of news needs different data sets.
example of newsfeed: 1) User "A" has added 2 new images (here I have to get user_name, user_avatar, ima开发者_Go百科ge_path_1, image_path_2 etc..) 2) User "B" has commented Users's "A" image (here I need User's "B" name, User's "B" avatar, image_path, comment_text, User's "A" name etc...)
See what I mean?
So what I want to do is to get data from MySQL Stored Procedure converted to JSON format.
{"news":
[
{news_type: "new_images", user_name: "john", user_avatar: "0002.jpg", image_path: "/images/00123.jpg"}
{news_type: "comment", user_name: "john", user_avatar: "0002.jpg", comment: "hello!"}
]
}
I can do this conversion in MySQL by "concat" and "group_concat"... I just want to ask your opinion if it's a good idea or not - and if not then what is a better way.
Thanks for your help!
UPDATE so finally I switched to MongoDB :)
This would not be wise. You should only use your database server for database work.
PHP will automatically do all the JSON encoding for you with json_encode() if you just pass it an associative array. No point reinventing the wheel or bogging down your database server.
It would be handy to have a stream output type though: less unmarshaling/marshaling. And if you need to support php and java consumers its more data handling. It'd seem like less work to hook up an output stream and call a stored procedure.
精彩评论