Php 将JSON解析为mySQL
我的PHP脚本中的get JSON字符串如下所示(包含任何对象的数组):Php 将JSON解析为mySQL,php,mysql,json,Php,Mysql,Json,我的PHP脚本中的get JSON字符串如下所示(包含任何对象的数组): 如何使用mySQL中的记录保存此数组中的每个JSON对象?使用JSON\u decode,然后使用数组的值构造insert语句使用JSON\u decode,然后使用数组的值构造insert语句 <?php $json = '[ { "source":"symbols/2/2.png", "ypos":133, "template":"8B82CA47-41D2
如何使用mySQL中的记录保存此数组中的每个JSON对象?使用JSON\u decode,然后使用数组的值构造insert语句使用JSON\u decode,然后使用数组的值构造insert语句
<?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);
}
试试这个:
<?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);
}
@Astraport检查答案:-)我以为我在什么地方回答了这个问题else@Astraport检查答案:-)我以为我只是在别的地方回答了这个问题。谢谢你,维玛尔纳特。代码有一些小错误,但这正是我想要的。谢谢你,vimalnath。代码有一些小错误,但这正是我所要寻找的。