简体   繁体   中英

PHP loop through JSON response and insert into MySQL

For a school project I want to loop through a JSON response and insert the data into a MySQL database.

The JSON response looks like this:

{"tableID":100965,"data":[{"updated_at":1506994152000,"prices":{"unstable_reason":"LOW_SALES","unstable":true,"sold":{"avg_daily_volume":null,"last_90d":67,"last_30d":52,"last_7d":15,"last_24h":1},"max":287.5,"avg":93.52,"min":51.01,"latest":87},"image":"IMGURL","db_name":"Test-Artikel","dbID":"123456789"},{"updated_at":1506994152000,"prices":{"unstable_reason":"LOW_SALES","unstable":true,"sold":{"avg_daily_volume":null,"last_90d":67,"last_30d":52,"last_7d":15,"last_24h":1},"max":287.5,"avg":93.52,"min":51.01,"latest":87},"image":"IMGURL","db_name":"Test-Artikel","dbID":"123456789"}],"__v":0,"createdAt":"2017-03-27T09:16:48.395Z"}

Every array should be a own row in the mysql database.

Currently i'm able to get one information out of the json file with this code:

<?php
$url = "linktojson";

//connect to database
//$pdo = new PDO('mysql:host=localhost;dbname=...', '...', '...');

//read the json file contents
$jsondata = file_get_contents($url);

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

echo('<pre>');
foreach ($data['data'] as $api_data) {
    echo $api_data['name'] . '<br/>';

}
?>

EDIT: Current Code:

$pdo = new PDO('mysql:host=localhost;dbname=XXX', 'XXX', 'XXX');
$yourJsonArray ="test.json";
$dataArray = json_decode(json_encode($yourJsonArray),true);
foreach($dataArray as $key => $value){
   $image = $key['image'];
   $statement = $pdo->prepare('INSERT INTO api_data (image) VALUES (?)'); 
   $statement->execute(array($image));
}

Error MSG: Warning: Invalid argument supplied for foreach() in test.php on line 29

Line 29 = foreach (got some old comments on top)

EDIT: Added var_dump($yourJsonArray); before foreach:

string(638) "{"tableID":100965,"data":[{"updated_at":1506994152000,"prices":{"unstable_reason":"LOW_SALES","unstable":true,"sold":{"avg_daily_volume":null,"last_90d":67,"last_30d":52,"last_7d":15,"last_24h":1},"max":287.5,"avg":93.52,"min":51.01,"latest":87},"image":"IMGURL","db_name":"Test-Artikel","dbID":"123456789"},{"updated_at":1506994152000,"prices":{"unstable_reason":"LOW_SALES","unstable":true,"sold":{"avg_daily_volume":null,"last_90d":67,"last_30d":52,"last_7d":15,"last_24h":1},"max":287.5,"avg":93.52,"min":51.01,"latest":87},"image":"IMGURL","db_name":"Test-Artikel","dbID":"123456789"}],"__v":0,"createdAt":"2017-03-27T09:16:48.395Z"}"
Warning: Invalid argument supplied for foreach() in test.php on line 30

EDIT: changed $dataArray to $dataArray = json_decode($yourJsonArray,true);

Error (4x): Warning: Illegal string offset 'image' in test.php on line 33

line 33 = $image = $key['image'];

EDIT: I dumped the $key Var and got this:

string(7) "tableID" string(4) "data" string(3) "__v" string(9) "createdAt"

How do i get the Elements of "data"?

Try like this :

$yourJsonArray = "You Json Array will come here";
//$dataArray = json_decode(json_encode($yourJsonArray),true);
$dataArray = json_decode($yourJsonArray,true); 
foreach($dataArray as $key => $value){
   //Here 0,1,2,3 Will be contained inside the $key variable.
   //Code to insert the data comes here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM