duandai0373 2012-06-29 07:21
浏览 6
已采纳

将JSON解析为mySQL

A get in my PHP script JSON string that looks like this (array with any objects):

[
    {
        "source":"symbols/2/2.png",
        "ypos":133,
        "template":"8B82CA47-41D2-D624-D6A2-37177CD82F28",
        "rotation":0,
        "type":"MyImage",
        "width":252,
        "depth":5,
        "height":159,
        "xpos":581
    },
    {
        "source":"symbols/2/2.png",
        "ypos":175,
        "template":"8B82CA47-41D2-D624-D6A2-37177CD82F28",
        "rotation":0,
        "type":"MyImage",
        "width":258,
        "depth":3,
        "height":163,
        "xpos":214
    },
    {
        "color":"0",
        "ypos":468.38,
        "fontSize":28,
        "xpos":156.95,
        "rotation":0,
        "type":"MyTextArea",
        "width":268.05,
        "depth":7,
        "height":244.62,
        "fontFamily":"Verdana Bold",
        "template":"8B82CA47-41D2-D624-D6A2-37177CD82F28"
    }
]

How i can save each JSON object in this array with a record in mySQL?

  • 写回答

2条回答 默认 最新

  • dqblm40280 2012-06-29 07:46
    关注

    Try this:

    <?php
    $json = '[
        {
            "source":"symbols/2/2.png",
            "ypos":133,
            "template":"8B82CA47-41D2-D624-D6A2-37177CD82F28",
            "rotation":0,
            "type":"MyImage",
            "width":252,
            "depth":5,
            "height":159,
            "xpos":581
        },
        {
            "source":"symbols/2/2.png",
            "ypos":175,
            "template":"8B82CA47-41D2-D624-D6A2-37177CD82F28",
            "rotation":0,
            "type":"MyImage",
            "width":258,
            "depth":3,
            "height":163,
            "xpos":214
        },
        {
            "color":"0",
            "ypos":468.38,
            "fontSize":28,
            "xpos":156.95,
            "rotation":0,
            "type":"MyTextArea",
            "width":268.05,
            "depth":7,
            "height":244.62,
            "fontFamily":"Verdana Bold",
            "template":"8B82CA47-41D2-D624-D6A2-37177CD82F28"
        }
    ]';
    //create a DB connection
    con = mysql_connect("localhost","username","password");
    mysql_connect _db('your_database',$con);
    
    
    $result = json_decode($json);
    foreach($result as $key => $value) {
        if($value) {
    
                //how to use json array to insert data in Database
            mysql_query("INSERT INTO tablename (source, ypos, template) VALUES ($value->source, $value->ypos,$value->template)");
        }
        mysql_close($con);
    }
    

    Note: But it is recommended to use PHP Data Objects(PDO) to do database operations. Check here

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料