从MySQL获取JSON数据树 [英] getting JSON data-tree from MySQL

查看:569
本文介绍了从MySQL获取JSON数据树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于Facebook新闻稿的新闻稿.

I have a newsfeed similar to Facebook's newsfeed.

这意味着我不需要弄成直"的行& cols来自MySQL的数据集,但更像是具有不同分支长度(和不同内容)的数据树".

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

如果我直截了当,我将在PHP中陷入很多嵌套循环-因为每种新闻类型需要不同的数据集.

If I go straight I will end up with lots of nested cycles in PHP - because each type of news needs different data sets.

新闻源示例: 1)用户"A"添加了2张新图片(在这里我必须获取user_name,user_avatar,image_path_1,image_path_2等.) 2)用户"B"已评论用户的"A"图像(在这里我需要用户的"B"名称,用户的"B"化身,image_path,comment_text,用户的"A"名称等...)

example of newsfeed: 1) User "A" has added 2 new images (here I have to get user_name, user_avatar, image_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...)

明白我的意思吗?

所以我要做的是将MySQL存储过程中的数据转换为JSON格式.

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!"}
            ]
      }

我可以通过"concat"和"group_concat"在MySQL中进行此转换...我只是想问您一个好主意-如果不是,那么有什么更好的方法.

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.

感谢您的帮助!

更新 所以最后我切换到MongoDB:)

UPDATE so finally I switched to MongoDB :)

推荐答案

这不是明智的选择.您应该只将数据库服务器用于数据库工作.

This would not be wise. You should only use your database server for database work.

如果您只是将关联数组传递给PHP,PHP就会使用json_encode()自动为您完成所有JSON编码.没必要重新发明轮子或使您的数据库服务器瘫痪.

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.

这篇关于从MySQL获取JSON数据树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆