Related
I want to fetch the below json data using php:
$str ='{
"site_Id": "1",
"site_Name": "Guajarat",
"site_Address": "Emami",
"site_StartDate": "10 / November / 2017 To 11 / November / 2017",
"Capacitor": [
{
"checkList_Id_PointNo": "1",
"checkList_Name": "APFC Panel doors and covers properly closed",
"checkList_Status": "Not OK",
"checkList_Remark": "Remark 1",
"checkList_Photo": "11Capacitor BankCapacitor Name1",
"fk_equipmentType": "Capacitor Bank"
},
{
"checkList_Id_PointNo": "2",
"checkList_Name": "APFC Panel door locks working properly.",
"checkList_Status": "Not OK",
"checkList_Remark": "Remark ",
"checkList_Photo": "11Capacitor BankCapacitor Name2",
"fk_equipmentType": "Capacitor Bank"
}
],
"DG": [
{
"checkList_Id_PointNo": "1",
"checkList_Name": "Substation earthing layout clearly making the position or earthing pits with identification number and the route of earthing lead / strip",
"checkList_Status": "Not OK",
"checkList_Remark": "Remark gg",
"checkList_Photo": "12EarthingDG Earthing1",
"fk_equipmentType": "Earthing"
},
{
"checkList_Id_PointNo": "2",
"checkList_Name": "All earhting pits should have identification number as indicated in earthing layout written over them with permanent paint",
"checkList_Status": "Not OK",
"checkList_Remark": "Remark gg",
"checkList_Photo": "12EarthingDG Earthing2",
"fk_equipmentType": "Earthing"
}
]
}';
Until now i have only coded for the Capacitor but i also need to fetch both Capacitor and DG from the same loop. Below is the foreach loop from which i have only fetch data of Capacitor but in that loop i also need to fetch DG at the same time.
foreach ($json['Capacitor'] as $field => $value) {
$id = $json['Capacitor'][$field]['checkList_Id_PointNo'];
$name = $json['Capacitor'][$field]['checkList_Name'];
$status = $json['Capacitor'][$field]['checkList_Status'];
$remark = $json['Capacitor'][$field]['checkList_Remark'];
$photo = $json['Capacitor'][$field]['checkList_Photo'];
echo $id.'<br>'.$name.'<br>'.$status.'<br>'.$remark.'<br>'.$photo.'<br><br>';
}
You can do it like below:-
<?php
$json = json_decode($str,true);
print_r($json);
foreach ($json as $value) {
if(is_array($value)){
foreach($value as $val){
$id = $val['checkList_Id_PointNo'];
$name = $val['checkList_Name'];
$status = $val['checkList_Status'];
$remark = $val['checkList_Remark'];
$photo = $val['checkList_Photo'];
echo "INSERT INTO table (id, name, status, remark, photo) VALUES ('$id', '$name', '$status','$remark','$photo')".PHP_EOL; // i have printed it so that you can see that it's printing perfectly.
}
}
}
Output:- https://eval.in/898497
I want to enter some details that included in JSON arrays to database.
Given below is my source code.
here is my data json array from the txt file
{
"reader_name":"Biboy Pogi",
"mac_address":"00:16:25:10:7E:85",
"tag_reads":[
{
"antennaPort":1,
"epc":"2015031687850100010105B5",
"tid":"E280110520005A8B952F0886",
"isHeartBeat":false
}
]
}
{
"reader_name":"Biboy Pogi",
"mac_address":"00:16:25:10:7E:85",
"tag_reads":[
{
"antennaPort":1,
"epc":"2015031687850100010105B5",
"tid":"E280110520005A8B952F0886",
"isHeartBeat":false
}
]
}
Assuming that you source data come in one json per row. You can do the iteration insert with the below sample code:
$fn = 'debug.txt';
$raw = file_get_contents($fn);
$raw = explode("\r\n", $raw);
foreach($raw as $row){
//force it to array for single item
if (substr($row, 0, 1) != '[')
$row = '[' . $row . ']';
$data_arr= json_decode($row, true);
foreach ($data_arr as $data){
$reader_name = $data['reader_name'];
$mac_address = $data['mac_address'];
$antennaPort = $data['tag_reads'][0]['antennaPort'];
$epc = $data['tag_reads'][0]['epc'];
$tid = $data['tag_reads'][0]['tid'];
$sql = "INSERT INTO tags(reader_name, mac_address, antennaPort, epc, tid)
VALUES('$reader_name', '$mac_address', '$antennaPort', '$epc', '$tid')";
if(!mysqli_query($con, $sql))
{
die('Error : ' . mysqli_error($con));
}
}
}
your json seems to be the problem.
try this:
[
{
"reader_name": "Biboy Pogi",
"mac_address": "00:16:25:10:7E:85",
"tag_reads": [
{
"antennaPort": 1,
"epc": "2015031687850100010105B5",
"tid": "E280110520005A8B952F0886",
"isHeartBeat": false
}
]
},
{
"reader_name": "Biboy Pogi",
"mac_address": "00:16:25:10:7E:85",
"tag_reads": [
{
"antennaPort": 1,
"epc": "2015031687850100010105B5",
"tid": "E280110520005A8B952F0886",
"isHeartBeat": false
}
]
}
]
You'r JSON is wrong and two times the same. Maybe you'll need it that way, but this one of them
{
"reader_name": "Biboy Pogi",
"mac_address": "00:16:25:10:7E:85",
"tag_reads": [
{
"antennaPort": 1,
"epc": "2015031687850100010105B5",
"tid": "E280110520005A8B952F0886",
"isHeartBeat": false
}
]
}
And you can't reach $antennaPort = $data['antennaPort']; that way. You have to call it like this
$antennaPort = $data['tag_reads']['antennaPort'];
$epc= $data['tag_reads'][0]['epc'];
$tid= $data['tag_reads'][0]['tid'];
EDIT
Insert into you'r database like this
mysqli_query($con,$sql)
After that, you should check for errors
if(!mysqli_query($con, $sql))
die ('Error in query: ' . mysqli_error($con));
Or you go in one line
mysqli_query($con, $sql) or die(mysqli_error());
So basically I want to parse a JSON file in PHP and insert the data into specific tables/columns. At the moment I have a working script but requires me to modify the JSON largely until it works. However, it won't end up working because the JSON data I'm collecting can vary in size having more data rows.
The JSON file is structured differently to most I have seen. Maybe because its output data from sensor units. I want to insert the data and the serial number into the data table, and have an error_log table where I can store the serial number and error messages as strings. How can I achieve this?
JSON File:
{
"device": {
"sn": 5165654,
"name": "FDI_AWS_DEMO",
"v": "2.7B3"
},
"channels": [
{
"code": "RH",
"name": "Relative Humidity",
"unit": "%"
},
{
"code": "AT",
"name": "Air Temperature",
"unit": "C"
},
{
"code": "MINVi",
"name": "Min voltage",
"unit": "V"
},
{
"code": "PTi",
"name": "Processor temperature",
"unit": "C"
},
{
"code": "SDB",
"name": "Network signal dB",
"unit": "dB"
},
{
"code": "LWS",
"name": "Leaf Wetness",
"unit": "%"
},
{
"code": "WSAV",
"name": "Wind Speed Avg",
"unit": "km/h"
},
{
"code": "WSMX",
"name": "Wind Speed Max",
"unit": "km/h"
},
{
"code": "WSMN",
"name": "Wind Speed Min",
"unit": "km/h"
},
{
"code": "PR_TOT",
"name": "PR Tot",
"unit": "mm"
},
{
"code": "RAIN",
"name": "Rain",
"unit": "mm"
},
{
"code": "FDI",
"name": "fdi",
"unit": "Unit"
},
{
"code": "DT",
"name": "Delta-T",
"unit": "C"
},
{
"code": "LAT",
"name": "Latitude",
"unit": "deg"
},
{
"code": "LON",
"name": "Longitude",
"unit": "deg"
},
{
"code": "WD",
"name": "Wind Direction",
"unit": "Degrees"
},
{
"code": "P1",
"name": "Par1",
"unit": ""
},
{
"code": "AVGCi",
"name": "Average Current",
"unit": "mA"
},
{}
],
"data": [
{
"$ts": 170801164400,
"$msg": "SD_FAIL;1"
},
{
"$ts": 170801170000,
"$msg": "WDT;WV01"
},
{
"$ts": 170801170000,
"$msg": "WDT;SDI12"
},
{
"$ts": 170801170000,
"$msg": "WDT;LWS"
},
{
"$ts": 170801170000,
"RH": 67.15,
"AT": 12.87,
"MINVi": 3.81,
"PTi": 23.4,
"LWS": "0*T",
"WSAV": 0,
"WSMX": 0,
"WSMN": 0,
"PR_TOT": 156,
"RAIN": 0,
"FDI": 0.239,
"DT": 2.881,
"WD": "0*T",
"P1": "0*T",
"AVGCi": 175
},
{}
]
}
PHP Code:
<?php
//connect to mysql db
$myConnection= mysqli_connect("localhost","root","******", "ii") or die ("could not connect to mysql");
//read the json file contents
$jsondata = file_get_contents('test.json');
//convert json object to php associative array
$data = json_decode($jsondata, true);
$id = $data['device']['sn'];
$ts = $data['data']['$ts'];
$RH = $data['data']['RH'];
$AT = $data['data']['AT'];
$MINVi = $data['data']['MINVi'];
$PTi = $data['data']['PTi'];
$SDB = $data['data']['SDB'];
$LWS = $data['data']['LWS'];
$WSAV = $data['data']['WSAV'];
$WSMX = $data['data']['WSMX'];
$WSMN = $data['data']['WSMN'];
$PR_TOT = $data['data']['PR_TOT'];
$RAIN = $data['data']['RAIN'];
$FDI = $data['data']['FDI'];
$DT = $data['data']['DT'];
$LAT = $data['data']['LAT'];
$LON = $data['data']['LON'];
$WD = $data['data']['WD'];
$P1 = $data['data']['P1'];
$AVGCi = $data['data']['AVGCi'];
//insert into mysql table
$sql = "INSERT INTO test(sn, date, RH, AT, MINVi, PTi, SDB, LWS, WSAV, WSMX, WSMN, PR_TOT, RAIN, FDI, DT, LAT, LON, WD, P1, AVGCi)
VALUES('$id', '$ts', '$RH','$AT', '$MINVi', '$PTi', '$SDB', '$LWS', '$WSAV', '$WSMX', '$WSMN', '$PR_TOT', '$RAIN', '$FDI', '$DT', '$LAT', '$LON', '$WD', '$P1', '$AVGCi')";
$query=mysqli_query($myConnection, $sql) or die(mysqli_error($myConnection));
?>
Tables Test data table and error_log table
JSON array var_dump - JSON var dump
Any help would be great
(After i get the general gist i want to incorporate PDO)
Do not convert json to associative array blindly. It creates more problems.
For accessing properties containing special characters or reserved
words use placeholders like $data->{'$ts'}
Loop through arrays and objects if needed.
Adding an auto increment id column to tables helps to store data
for one device.
It is a good idea to add time to error_log table as well
Tested bellow short version of your original question and it works.
<?php
$_user = 'root';
$_password= 'root';
$_db = 'localtest';
$_host = 'localhost';
$_port = 3306;
$con = new mysqli($_host, $_user, $_password, $_db) or die(mysql_error);
//read the json file contents
$jsondata = file_get_contents('test.json');
//do not convert to array
$json = json_decode($jsondata);
$id = $json->device->sn;
foreach($json->data as $key => $data){
if(empty($data) || !isset($data->{'$ts'})){
continue;
}
if (isset($data->{'$msg'})){
$msg = $data->{'$msg'};
$time = $data->{'$ts'};
$sql="INSERT into error_log (sn, time, MSG) VALUES (?,?,?); ";
$stmt = $con-> prepare($sql);
$stmt -> bind_param("iss", $id,$time, $msg);
$stmt -> execute();
}else{
$time = (isset($data->{'$ts'}))? $data->{'$ts'}:'';
$RH = (isset($data->RH))? $data->RH:'';
$AT = (isset($data->AT))? $data->AT:'';
$MINVi = (isset($data->MINVi))? $data->MINVi:'';
//insert into mysql table
$sql="INSERT into test (sn, date, RH, AT, MINVi) VALUES (?,?,?,?,?); ";
$stmt = $con-> prepare($sql);
$stmt -> bind_param("issss", $id,$time,$RH,$AT,$MINVi);
$stmt -> execute();
}
}
mysqli_close($con);
?>
looks like the json decode is turning the array into one that is object based...run the $data through the function below to turn it into more of a relational array that is setup the way you are trying to read it....
I cannot find the function i had for this at the moment but If you read the array more like this:
$newvar = $data->node;
//to echo this try this...
echo '<pre style="text-align:left;">';
print_r($data);
echo '</pre>';
OR try fb logs for php to see the arrays in console of ya browser - use firephp for the addon.
it should work just fine...
Hope that helps.
Consider building dynamic SQL calls conditionally depending on the error output. The array keys create the columns in INSERT clause and array values are quote wrapped in VALUES clause. Below echoes the sql statements just to demonstrate.
This should work for any change to JSON suffice no other new keys are added. Also, the array_splice is used to remove the $ts value the second time since you pass it in date column.
$jsondata = file_get_contents('Input.json');
$data = json_decode($jsondata, true);
$id = $data['device']['sn'];
foreach ($data['data'] as $k=>$v){
if (array_key_exists("\$msg",$v) & !empty($v)){
$sql = "INSERT INTO error_log (sn, msg)"
." VALUES('$id', '". $v["\$msg"] ."')";
echo $sql."\n";
// INSERT INTO error_log (sn, msg) VALUES('5165654', 'SD_FAIL;1')
// INSERT INTO error_log (sn, msg) VALUES('5165654', 'WDT;WV01')
// INSERT INTO error_log (sn, msg) VALUES('5165654', 'WDT;SDI12')
// INSERT INTO error_log (sn, msg) VALUES('5165654', 'WDT;LWS')
$query = mysqli_query(...);
}
if (!array_key_exists("\$msg",$v) & !empty($v)) {
$keysArray = array_keys($v);
$keysArray = array_splice($keysArray, 1);
$vVals = array_splice($v, 1);
$sql = "INSERT INTO test(sn, date, ". implode(", ", $keysArray) .")\n"
." VALUES('$id', '". $v['$ts'] ."',". implode("', '", $vVals) .")";
echo $sql."\n";
// INSERT INTO test(sn, date, RH, AT, MINVi, PTi, LWS, WSAV, WSMX, WSMN,
// PR_TOT, RAIN, FDI, DT, WD, P1, AVGCi)
// VALUES('5165654', '170801170000',67.15', '12.87', '3.81', '23.4', '0*T',
// '0', '0', '0', '156', '0', '0.239', '2.881', '0*T', '0*T', '175)
$query=mysqli_query(...)
}
}
Maybe something like this can get you on track... One thing I did notice is there doesn't appear to be an SDB entry in the given JSON data (FYI).
<?php
//connect to mysql db
$myConnection= mysqli_connect("localhost","root","******", "ii") or die ("could not connect to mysql");
//read the json file contents
$jsondata = file_get_contents('test.json');
//convert json object to php associative array
$data = json_decode($jsondata, true);
// Make sure $data has values
if (empty($data)) {
// Process error here
}
else {
// Make sure the proper keys have been set
if (!(isset($data['device']) && !empty($data['device']) && isset($data['data']) && !empty($data['data']))) {
// Process error here if not set
}
// If so, make sure the sn key has been set
elseif (!isset($data['device']['sn']) || empty($data['device']['sn'])) {
// Process error here if not set
}
else {
$data_arr = $data['data'];
foreach ($data_arr as $key => $arr) {
// Iterate through for loop
for ($i = 0; $i < count($data_arr); $i++) {
// Make sure every single key is set
if (!(isset($data_arr[$i]['$ts']) && isset($data_arr[$i]['RH']) && isset($data_arr[$i]['AT']) && isset($data_arr[$i]['MINVi']) && isset($data_arr[$i]['PTi']) && isset($data_arr[$i]['SDB']) && isset($data_arr[$i]['LWS']) && isset($data_arr[$i]['WSAV']) && isset($data_arr[$i]['WSMX']) && isset($data_arr[$i]['WSMN']) && isset($data_arr[$i]['PR_TOT']) && isset($data_arr[$i]['RAIN']) && isset($data_arr[$i]['FDI']) && isset($data_arr[$i]['DT']) && isset($data_arr[$i]['LAT']) && isset($data_arr[$i]['LON']) && isset($data_arr[$i]['WD']) && isset($data_arr[$i]['P1']) && isset($data_arr[$i]['AVGCi']) && isset($data_arr[$i]['DT']) && isset($data_arr[$i]['DT']))) {
// Process error here if not set
}
else {
// If all is well, perform the query
$id[$key][$i] = $arr['device']['sn'];
$ts[$key][$i] = $data_arr[$i]['$ts'];
$RH[$key][$i] = $data_arr[$i]['RH'];
$AT[$key][$i] = $data_arr[$i]['AT'];
$MINVi[$key][$i] = $data_arr[$i]['MINVi'];
$PTi[$key][$i] = $data_arr[$i]['PTi'];
$SDB[$key][$i] = $data_arr[$i]['SDB'];
$LWS[$key][$i] = $data_arr[$i]['LWS'];
$WSAV[$key][$i] = $data_arr[$i]['WSAV'];
$WSMX[$key][$i] = $data_arr[$i]['WSMX'];
$WSMN[$key][$i] = $data_arr[$i]['WSMN'];
$PR_TOT[$key][$i] = $data_arr[$i]['PR_TOT'];
$RAIN[$key][$i] = $data_arr[$i]['RAIN'];
$FDI[$key][$i] = $data_arr[$i]['FDI'];
$DT[$key][$i] = $data_arr[$i]['DT'];
$LAT[$key][$i] = $data_arr[$i]['LAT'];
$LON[$key][$i] = $data_arr[$i]['LON'];
$WD[$key][$i] = $data_arr[$i]['WD'];
$P1[$key][$i] = $data_arr[$i]['P1'];
$AVGCi[$key][$i] = $data_arr[$i]['AVGCi'];
$sql[$key][$i] = "INSERT INTO test(sn, date, RH, AT, MINVi, PTi, SDB, LWS, WSAV, WSMX, WSMN, PR_TOT, RAIN, FDI, DT, LAT, LON, WD, P1, AVGCi)
VALUES('{$id[$key][$i]}', '{$ts[$key][$i]}', '{$RH[$key][$i]}','{$AT[$key][$i]}', '{$MINVi[$key][$i]}', '{$PTi[$key][$i]}', '{$SDB[$key][$i]}', '{$LWS[$key][$i]}', '{$WSAV[$key][$i]}', '{$WSMX[$key][$i]}', '{$WSMN[$key][$i]}', '{$PR_TOT[$key][$i]}', '{$RAIN[$key][$i]}', '{$FDI[$key][$i]}', '{$DT[$key][$i]}', '{$LAT[$key][$i]}', '{$LON[$key][$i]}', '{$WD[$key][$i]}', '{$P1[$key][$i]}', '{$AVGCi[$key][$i]}')";
//insert into mysql table
$query[$key][$i] = mysqli_query($myConnection, $sql[$key][$i]) or die(mysqli_error($myConnection));
}
}
}
}
}
?>
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?
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
Use json_decode and then construct a insert statement using the values of the array
I'm a newbie programmer trying to find my way in the world. I've got my hands on JSON data that I'm trying to parse out into SQL statements to populate multiple database tables. I would like to loop through each dimension of the array and pull out specific parts of it to create an INSERT statement I can just pass to MySQL. I'm not sure if this is the best way to populate separate tables with data from one JSON file but it's the only thing I can think of. MySQL tables are separated so there is a table for a person, a table for address type, a table for address, a table for phone, a table for email etc. This is to account for a person record having numerous phone numbers, email addresses etc.
I have been able to decode the JSON from an external URL. Here is the code and a sample of the output using print_r.
$json_string = 'http://....';
$jsondata = file_get_contents($json_string);
$data = json_decode($jsondata, TRUE);
1 Record Sample:
Array ( [objects] => Array ( [0] => Array ( [first_name] => Anthony [last_name] => Perruzza [name] => Anthony Perruzza [elected_office] => City councillor [url] => http://www.toronto.ca/councillors/perruzza1.htm [gender] => [extra] => Array ( ) [related] => Array ( [boundary_url] => /boundaries/toronto-wards/york-west-8/ [representative_set_url] => /representative-sets/toronto-city-council/ ) [source_url] => http://www.toronto.ca/councillors/perruzza1.htm [offices] => Array ( [0] => Array ( [tel] => 416-338-5335 ) ) [representative_set_name] => Toronto City Council [party_name] => [district_name] => York West (8) [email] => councillor_perruzza#toronto.ca [personal_url] => [photo_url] => ) ) [meta] => Array ( [next] => /representatives/?limit=1&offset=1 [total_count] => 1059 [previous] => [limit] => 1 [offset] => 0 ) )
JSON Code Sample:
{"objects": [
{"first_name": "Keith",
"last_name": "Ashfield",
"name": "Keith Ashfield",
"elected_office": "MP",
"url": "http://www.parl.gc.ca/MembersOfParliament/ProfileMP.aspx?Key=170143&Language=E",
"gender": "",
"extra": {},
"related": {
"boundary_url": "/boundaries/federal-electoral-districts/13003/",
"representative_set_url": "/representative-sets/house-of-commons/"
},
"source_url": "http://www.parl.gc.ca/MembersOfParliament/MainMPsCompleteList.aspx?TimePeriod=Current&Language=E",
"offices": [
{ "type": "legislature",
"fax": "613-996-9955",
"postal": "House of Commons\nOttawa, Ontario\nK1A 0A6",
"tel": "613-992-1067"
},
{ "type": "constituency",
"fax": "506-452-4076",
"postal": "23 Alison Blvd (Main Office)\nFredericton, New Brunswick\nE3C 2N5",
"tel": "506-452-4110"
}
],
"representative_set_name": "House of Commons",
"party_name": "Conservative",
"district_name": "Fredericton",
"email": "keith.ashfield#parl.gc.ca",
"personal_url": "",
"photo_url": "http://www.parl.gc.ca/MembersOfParliament/Images/OfficialMPPhotos/41/AshfieldKeith_CPC.jpg"
}
],
"meta": {
"next": "/representatives/house-of-commons/?limit=1&offset=1",
"total_count": 307,
"previous": null,
"limit": 1,
"offset": 0
}
}
Any help you can offer would be greatly appreciated. I've been pulling my hair out for the last few days trying to figure it out.
I've tried customizing code like the following to make it work but I haven't been able to hit the sweet spot. Please not, this code doesn't reference my data or variables. I deleted what didn't work for me. I'm just including it to give you an idea what I've tried.
foreach ($data as $item) {
echo $item->{'first_name'} . "<br/>";
echo $item->{'last_name'};
}
If you could point me in the direction of being able to parse out data from any level of the array it would be greatly appreciated.
Best,
S
AFAIK, it is not possible to insert into several tables with one insert. Moreover, you need to preserve data integrity, so related tables would have right foreign keys.
The general idea is to iterate through the data, insert records and remember inserted ids, then write them as corresponding foreign keys.
You iterate thru your objects, insert all primitive properties as fields, then get an id using mysql_last_insert_id, then while saving offices (or their details) put that id as their related object id.
E.g. we have the following JSON.
{"authors": [
{"first_name": "John",
"last_name": "Doe",
"books": [{
"title": "Capture the flag",
"ISBN": "123-456789-12345",
},{
"title": "Deathmatch",
"ISBN": "123-456789-12346",
}]
]}
Then we insert that data with the following code:
foreach ($data as $author) {
mysql_query("INSERT INTO `authors` (`first_name`, `last_name`), VALUES('{$author->first_name}', '{$author->last_name}') ");
$author_id = mysql_last_insert_id();
foreach ($author->books as $book) {
mysql_query("INSERT INTO `books` (`title`, `isbn`, `author_id`), VALUES('{$book->title}', '{$book->isbn}', '{$author_id}') ");
}
}
This is for case you have auto-increment for id's in tables.
Of course, you'll need to validate and escape data before insertion etc.
Here is something that you can use to get the structure of a json response. It works recursively so that it will create an entry to for each object as well as an entry in a separate table for each property of each object. I hope to get others feedback/enhancement on this as well to turn it into create sql statements.
class DataDriller {
var $ext_obj_to_parse;
var $ext_type_name;
var $data;
var $recurse;
var $ext_type_id;
var $ext_related_id;
var $type_id;
var $auto_create;
var $sql;
var $error;
var $controller;
var $ExtType;
var $ExtStructure;
var $link;
var $ext_source_id;
function init($ExtType, $ExtStructure) {
$this->ExtType = $ExtType;
$this->ExtStructure = $ExtStructure;
}
function setup($ext_obj_to_parse, $ext_type_name, $ext_type_id = false, $ext_related_id = false, $auto_create = true, $ext_source_id) {
$this->ext_obj_to_parse = $ext_obj_to_parse;
$this->ext_type_name = $ext_type_name;
$this->ext_type_id = $ext_type_id;
$this->auto_create = $auto_create;
$this->error = false;
$this->ext_related_id = $ext_related_id;
$this->ext_source_id = $ext_source_id;
if ($this->get_ext_type_data() === false) {
if ($this->type_handling() === false) {
$this->error_data();
}
}
if (gettype($this->ext_obj_to_parse) == "object" || gettype($this->ext_obj_to_parse) == "array") {
$this->to_struct();
} else {
//single variable and data
$this->data[$this->ext_type_name] = gettype($this->ext_obj_to_parse);
$this->sql = "replace into ext_structures (name, data_type, ext_type_id) values ('$this->ext_type_name', '" . gettype($this->ext_obj_to_parse) . "', " . $this->ext_type_id . ")";
$this->sql_it();
}
}
function get_ext_type_data() {
if (is_numeric($this->ext_type_id)) {
return true;
} else if (strlen($this->ext_type_name) > 0) {
$this->sql = "select id From ext_types where name = '" . $this->ext_type_name . "' limit 1";
$this->ext_type_id = $this->sql_it('id');
return $this->ext_type_id;
} else {
return false;
}
}
function type_handling() {
if ($this->auto_create == true && gettype($this->ext_type_name) === "string") {
//$this->sql = "replace into types (name) values ('$this->ext_type_name')";
//
//$this->type_id = $this->sql_it();
//if ($this->type_id !== 0) {
//if ($this->ext_related_id) {
$this->sql = "insert into ext_types (name, ext_source_id, parent_id) values ( '$this->ext_type_name', $this->ext_source_id, '$this->ext_related_id')";
$this->ext_type_id = $this->sql_it();
$this->sql = "replace into ext_type_rel (ext_type_id_1, ext_type_id_2) values ($this->ext_type_id, $this->ext_related_id)";
$this->sql_it();
/*} else {
$this->error = "Unable to obtain typeid from insert";
$this->error_data();
return false;
}*/
}
//}
}
function to_struct() {
//keys are not objects but values can be
//always display keys, when value object - increase spacer - call self - reiterate
// if value is not object complete
foreach ($this->ext_obj_to_parse as $key => $value) {
if (gettype($value) == "object" || gettype($value) == "array") {
//check to see if object exists within the database with the data definitions and methods
//there are no existing data structure insert
//recurse into the drill-down again if it does not exist
if (is_numeric($key) || $key == "data" || $key == "statuses") {
$this->recurse = new DataDriller();
if (!$this->ext_related_id > 0){ $this->ext_related_it = $this->ext_type_id; }
$this->recurse->setup($value, $this->ext_type_name, $this->ext_type_id, $this->ext_related_id, true, $this->ext_source_id);
} else {
$this->recurse = new DataDriller();
$this->recurse->setup($value, $key, false, $this->ext_type_id, true, $this->ext_source_id);
}
$this->data[$key] = $this->recurse->data;
unset($this->recurse);
//this is where we insert the relationship between objects here
} else {
//not an ojbect just a field of the existing object
$this->data[$key] = gettype($value);
$this->sql = "replace into ext_structures (name, data_type, ext_type_id) values ('$key', '" . gettype($value) . "', " . $this->ext_type_id . ")";
$this->sql_it();
}
}
}
function sql_it($field_name = false) {
$VARDB_server = '192.168.10....';
$VARDB_port = '3306';
$VARDB_user = 'user';
$VARDB_pass = 'pass';
$VARDB_database = 'db_name';
$this->link = mysql_connect("$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass");
if (!$this->link) {
echo 'MySQL connect ERROR: ' . mysql_error();
die();
}
$res = mysql_select_db("$VARDB_database");
if (!$res) {
echo mysql_error();
}
$res = mysql_query($this->sql, $this->link);
if (mysql_error()) {
$this->error = mysql_error() . " MYSQL reported an error " . $this->sql;
CakeLog::write('datadriller', $this->sql . " error? " . mysql_error());
die();
}
if ($field_name === false) {
if (strpos($this->sql, 'insert') !== false || strpos($this->sql, 'replace') !== false) {
$id = mysql_insert_id();
return $id;
} else {
$this->error = "field name is requeired for getting results";
$this->error_data();
return false;
}
} else {
if (mysql_num_rows($res) > 0) {
$r = mysql_fetch_array($res);
mysql_free_result($res);
if (array_key_exists($field_name, $r)) {
return $r[$field_name];
} else {
$this->error = "field name does not exist in result set";
$this->error_data();
return false;
}
} else {
$this->error = "select statement returned no data ";
return false;
}
}
}
function error_data() {
echo "<B> $this->error MySQL error? <font color=red>" . mysql_error() . " </font> SQL: $this->sql </b><BR><BR>\n";
echo "DUMP DATA\n";
echo "<pre>";
var_dump($this->data);
echo "RECURSED OBJECT \n\n";
var_dump($this->recurse);
echo "</pre>";
}
function JSONTOInsertSQL($table,$obj){
$keys = implode('`,`', array_map('addslashes', array_keys($obj)));
$values = implode("','", array_map('addslashes', array_values($obj)));
return "INSERT INTO `$table` (`$keys`) VALUES ('$values')";
}