doudeng2057 2016-04-13 10:55
浏览 54

如何在Json中获取PHP数据库值?

I have to create Matrix tree view in my project. So am plan to use json. My question is how to fetch PHP values in Json ?. I did static matrix tree but i want dynamic. Thank you for advance.

My code is following:

<?php
include('db.php');
$select = mysql_query("select * from table1");
while($row = mysql_fetch_array($select))
{
?>
{
 "name": "A", // Here database values come $row['name']; 
 "children": [



  {
   "name": "B",
   "children": [
    {"name": "B-1"}
   ]
  },
  {
   "name": "C",
   "children": [
    {"name": "C-1", "size": 1082},

    {"name": "C-2", "size": 1681}
   ]
  },



  {
   "name": "D",
   "children": [
    {
     "name": "D-1",
     "children": [
      {"name": "D-1 1", "size": 1302},


      {"name": "D-1 2", "size": 6703}
     ]
    },

   {"name": "D-2", "size": 16540}
   ]
  }
 ]
}
<?php
}
?>
  • 写回答

2条回答 默认 最新

  • dongpou7275 2016-04-13 10:58
    关注

    You can just retrieve data from your database and store it in arrays like you normally would. Then call PHP's built in function json_encode() to transform your PHP array into json (assuming your PHP array is well formed (which should be the case if you get it out of a database)).

    You could argue that this is slower because you're iterating over the data twice instead of once, but it shouldn't matter, the complexity remains the same.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c