问题描述:

I have an API that returns JSON data that I want to import to my DB. I know that it's possible to download the JSON file, convert it to SQL and then import it to the DB.

The data from the API contains 19 columns without first row names. I thought I should create a table in my DB named "test" that contains these 19 columns named "apitest" and then fill it with the data from the API.

<?php

$con = mysql_connect("localhost","xxxx","xxxx");

mysql_select_db("test", $con);

$url = "https://bo-api.xxxx.io/v1/be/client/2015-07-01";

$json = file_get_contents($url);

$result = json_decode($json,true);

foreach($result as $key => $value)

{

if($value)

{

mysql_query("INSERT INTO apitest(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17,c18,c19)

VALUES ($value->c1,

$value->c2,

$value->c3,

$value->c4,

$value->c5,

$value->c6,

$value->c7,

$value->c8,

$value->c9,

$value->c10,

$value->c11,

$value->c12,

$value->c13,

$value->c14,

$value->c15,

$value->c16,

$value->c17,

$value->c18,

$value->c19");

}

mysql_close($con);

}

?>

相关阅读:
Top