I need to extract data from a JSON fila and insert it in MySQL with PHP. This is the code,
<?php
$connect = mysqli_connect("localhost", "root", "pass", "dbname"); //Connect PHP to MySQL Database
$query = '';
$table_data = '';
$filename = "organizations.json";
$data = file_get_contents($filename); //Read the JSON file in PHP
$array = json_decode($data, true); //Convert JSON String into PHP Array
foreach($array as $row) //Extract the Array Values by using Foreach Loop - "Inserto todos los campos en la tabla pero no muestro el 'id' en la web"
{
$query .= "INSERT INTO organizations(id, displayName, created) VALUES ('".$row["id"]."', '".$row["displayName"]."', '".$row["created"]."'); "; // Make Multiple Insert Query
$table_data .= '
<tr>
<td>'.$row["displayName"].'</td>
<td>'.$row["created"].'</td>
</tr>
'; //Data for display on Web page
}
The code works fine with the following json file:
[
{
"id": "Y2lzY29z",
"name": "Enterprise Edition",
"totalUnits": 1000,
"consumedUnits": 1
},
{
"id": "MGUzZjBj",
"name": "Messaging",
"totalUnits": 1000,
"consumedUnits": 0
}
]
But it doesn't work with the following json file:
{
"items": [
{
"id": "Y2lzY29z",
"name": "Enterprise Edition",
"totalUnits": 1000,
"consumedUnits": 1
},
{
"id": "MGUzZjBj",
"name": "Messaging",
"totalUnits": 1000,
"consumedUnits": 0
}
]
}
I don't know how to write the query correctly
$query .= "INSERT INTO organizations
(id, displayName, created)
VALUES ('".$row["id"]."', '".$row["displayName"]."',
'".$row["created"]."');
Thanks!