如何使用PHP foreach循环将多个嵌套的json对象插入到mysql中?

时间:2016-06-25 11:39:08

标签: php mysql json foreach

{
  {
    "empid":805475,
    "personal":{
        "name":"ABC",
        "gender":"Male",
        "age":28,
        "address":{
        "streetaddress":"Subhash Nagar",
        "city":"Agra",
        "state":"Uttarpradesh",
        "postalcode":209111
        }
    },
    "profile":{
        "designation":"Coder",
        "department":" Software dev."
    }
},
{
    "empid":814475,
    "personal":{
        "name":"PQR",
        "gender":"Male",
        "age":18,
        "address":{
        "streetaddress":"Subhash Nagar",
        "city":"Delhi",
        "state":"Delhi",
        "postalcode":110022
        }
    },
    "profile":{
        "designation":"Coder",
        "department":" Software dev."
    }
},
{
    "empid":805795,
    "personal":{
        "name":"MNP",
        "gender":"Male",
        "age":38,
        "address":{
        "streetaddress":"Subhash Nagar",
        "city":"Meerut",
        "state":"Uttarpradesh",
        "postalcode":209111
        }
    },
    "profile":{
        "designation":"Coder",
        "department":" Software dev."
    }
},
{
    "empid":805197,
    "personal":{
        "name":"AMN",
        "gender":"Male",
        "age":29,
        "address":{
        "streetaddress":"Subhash Nagar",
        "city":"Mathura",
        "state":"Uttarpradesh",
        "postalcode":546125
        }
    },
    "profile":{
        "designation":"Coder",
        "department":" Software dev."
    }
  }    
}

这是我想用PHP保存到Mysql的json数据。 这是我的代码只适用于单个json对象(仅适用于例如ABC),但显示多个对象的错误。 PHP代码:

//convert json object to php associative array
$data = json_decode($json, true);

//get the employee details
$id = $data['empid'];
$name = $data['personal']['name'];
$gender = $data['personal']['gender'];
$age = $data['personal']['age'];
$streetaddress = $data['personal']['address']['streetaddress'];
$city = $data['personal']['address']['city'];
$state = $data['personal']['address']['state'];
$postalcode = $data['personal']['address']['postalcode'];
$designation = $data['profile']['designation'];
$department = $data['profile']['department'];

//insert into mysql table
$sql = "INSERT INTO tbl_emp(empid, empname, gender, age, streetaddress, city, state, postalcode, designation, department)
VALUES('$id', '$name', '$gender', '$age', '$streetaddress', '$city', '$state', '$postalcode', '$designation', '$department')";
if(!mysql_query($sql,$con))
{
    die('Error : ' . mysql_error());
}

&GT?; 所以这是为一个对象工作的php代码,但我不知道如何将整个json文件数据一次插入到mysql DB中。

1 个答案:

答案 0 :(得分:0)

你的桌子怎么样?您是否希望从JSON数据构建S​​QL查询字符串?在下面的代码中,有很多假设可能符合或不符合您的意图,但仍然适用于初学者:

    <?php
        $jsonData = '[
          {
            "empid"     : 805475,
            "personal"  : {
                "name"      : "ABC",
                "gender"    : "Male",
                "age"       : 28,
                "address"   : {
                    "streetaddress" : "Subhash Nagar",
                    "city"          : "Agra",
                    "state"         : "Uttarpradesh",
                    "postalcode"    : 209111
                }
            },
            "profile":  {
                "designation"   : "Coder",
                "department"    : " Software dev."
            }
        },

        {
            "empid"     : 814475,
            "personal"  :{
                "name"      : "PQR",
                "gender"    : "Male",
                "age"       : 18,
                "address"   : {
                    "streetaddress" : "Subhash Nagar",
                    "city"          : "Delhi",
                    "state"         : "Delhi",
                    "postalcode"    : 110022
                }
            },
            "profile"   :{
                "designation"   : "Coder",
                "department"    : " Software dev."
            }
        },

        {
            "empid"     : 805795,
            "personal"  : {
                "name"      : "MNP",
                "gender"    : "Male",
                "age"       : 38,
                "address"   : {
                    "streetaddress":"Subhash Nagar",
                    "city"          : "Meerut",
                    "state"         : "Uttarpradesh",
                    "postalcode"    : 209111
                }
            },
            "profile"   :{
                "designation"   : "Coder",
                "department"    : " Software dev."
            }
        },

        {
            "empid"     :805197,
            "personal"  : {
                "name"      : "AMN",
                "gender"    : "Male",
                "age"       : 29,
                "address"   : {
                    "streetaddress" : "Subhash Nagar",
                    "city"          : "Mathura",
                    "state"         : "Uttarpradesh",
                    "postalcode"    : 546125
                }
            },
            "profile"   : {
                "designation"   : "Coder",
                "department"    : " Software dev."
            }
          }
    ]';


        //  WE ASSUME YOUR TABLE-NAME IS my_table JUST FOR DEMONSTRATION:
        $tblName    = "`my_table`";

        // CONVERT JSON DATA TO NATIVE PHP OBJECT
        $objJson    = json_decode($jsonData);

        // CREATE A VARIABLE TO HOLD THE SQL (QUERY STRING)
        $arrSQL     = array();


        foreach ($objJson as $key => $data) {
            $tmpSQL    = "INSERT INTO " . $tblName . "(empid, name, gender, age, streetaddress, city, state, postalcode, designation, department)";
            $tmpSQL   .= " VALUES('{$data->empid}', '{$data->personal->name}', '{$data->personal->gender}', '{$data->personal->age}', ";
            $tmpSQL   .= "'{$data->personal->address->streetaddress}', '{$data->personal->address->city}', '{$data->personal->address->state}', '{$data->personal->address->postalcode}', ";
            $tmpSQL   .= "'{$data->profile->designation}', '{$data->profile->department}' )";
            $arrSQL[]  = $tmpSQL;
        }

        // NOW YOU CAN JUST USE IMPLODE TO CREATE THE STRING EQUIVALENT OF THE SQL QUERY:
        $strSQL = implode(";\n\n", $arrSQL);

        var_dump($arrSQL);
        var_dump($strSQL);

测试HERE