douwei3280 2014-06-24 19:13 采纳率: 0%
浏览 169
已采纳

使用PHP在MYSQL中插入JSON值

I've this JSON string:

$json = '{
    "bigprodlist": {
        "prods": [
            {
                "code": 55,
                "name": "Comix Book",
                "link": "weblink"
            },
            {
                "code": 85,
                "name": "IT Book",
                "link": "weblink"
            },
            {
                "code": 95,
                "name": "Manga Book",
                "link": "weblink"
            }

        }
    }';

I'd like to print every single entry on a webpage using php and then save these entries on a mysql db. In the db there is already a "code", "name" and "link" field..

This is what I've tried without luck (to print the stuff on a page):

$obj = json_decode($json,true);
echo ($obj["bigprodlist"]["prods"][0]["name"]);

Thank you very much for the help

  • 写回答

3条回答 默认 最新

  • dream198731 2014-06-24 19:27
    关注

    First, fix your JSON missing end bracket that makes JSON decoding fail (add the ] after the prods data ), then expand your echo statement with some foreach loops to get the data printed. This is only a simple example to get you on the right track:

    foreach ($obj["bigprodlist"]["prods"] as $p):
        echo "<div>";
        foreach ($p as $name=>$value):
            echo "<span>".$name.": ".$value."</span>";
        endforeach;
       echo "</div>";
    endforeach;
    

    You can then use the same loop procedure to get the data into your DB.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换