数据未从JSON插入数据库

时间:2018-01-04 20:50:23

标签: php mysql json

我将数据从Json存储到db。一些rows [] elements []正在插入,但这是一个随机调用。 每次刷新后,第一个obj只存储,有时2个元素。 在查询中所有数据都正确但没有执行

This is the JSON file, I only want data from row[i]element[i] in diagonal.

This is the data I gathered from JSON file.

This is the data passing to the query but not inserted.

我只想要来自row [i] element [i]的对角线数据。

<?php
$url1 = $_SERVER['REQUEST_URI'];
header("Refresh: 1800; URL=$url1");
try {
$db = new PDO('mysql:host=localhost; dbname=distancejson', 'root', '');
} catch (PDOException $e) {die();}
mysql_error());
$url = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=33.712515,73.051294|33.704902,73.057019|33.697274,73.062723|33.689659,73.068459|33.682035,73.074173|33.674414,73.079879|33.666776,73.085611|33.659143,73.091299|33.651526,73.097076|33.643880,73.102775&destinations=33.704902,73.057019|33.697274,73.062723|33.689659,73.068459|33.682035,73.074173|33.674414,73.079879|33.666776,73.085611|33.659143,73.091299|33.651526,73.097076|33.643880,73.102775|33.636243,73.108493&mode=driving&departure_time=now&key=AIzaSyBmRTOahiTFM83GPiTUGo7NWTFbmL0U3yw';

$jsondata = json_decode(json_encode(file_get_contents($url)), true);

$data = json_decode($jsondata, true);
$size = sizeof($data);
for ($i = 0; $i < 10; $i++) {
echo "<br>";
for ($j = $i; $j < 10; $j++) {
    $intervalnumber = 1 + $i;
    $destination_addresses = $data['destination_addresses'][$j];
    $origin_addresses = $data['origin_addresses'][$j];
    $distance = $data['rows'][$i]['elements'][$j]['distance']['value'];
    $duration = $data['rows'][$i]['elements'][$j]['duration']['value'];
    $duration_in_traffic_text = $data['rows'][$i]['elements'][$j]['duration_in_traffic']['text'];
    $duration_in_traffic_value = $data['rows'][$i]['elements'][$j]['duration_in_traffic']['value'];
 $query = "INSERT INTO intervaltime(intervalnumber,destinationname,originname,distancevalue,durationintrafficvalue,durationintraffictext) VALUES(:intervalnumber,:destination_addresses,:origin_addresses,:distance,:duration_in_traffic_value,:duration_in_traffic_text)";

$pdoResult = $db->prepare($query);

$pdoExec = $pdoResult->execute(array(
"intervalnumber" => $intervalnumber,
"destination_addresses" => $destination_addresses,
"origin_addresses" => $origin_addresses,
"distance" => $distance,
"duration_in_traffic_value" => $duration_in_traffic_value,
"duration_in_traffic_text" => $duration_in_traffic_text
));

if ($pdoExec) {
echo("data Inserted!");
} else {
echo("Data not Inserted!");
}   

/ *
    $ query =(&#34; INSERT INTO intervaltime(database_parameters)VALUES(&#39; $ intervalnumber&#39;,&#39; $ destination_addresses&#39;,&#39; $ origin_addresses&#39;,&#39; $距离&#39;&#39; $ duration_in_traffic_value&#39;&#39; $ duration_in_traffic_text&#39;)&#34);         $ pdoResult = $ db-&gt; prepare($ query);         $ pdoExec = $ pdoResult-&gt; execute();         if($ pdoExec){             echo(&#39;数据已插入!&#39;);         } else {             echo(&#39;数据未插入!&#39;);         }         //关闭数据库连接         echo&#34;
&#34 ;;         echo&#34;
&#34;;
        打破;}}         $ db = null;         ?&GT;

1 个答案:

答案 0 :(得分:0)

请你这样尝试你的请求(使用prepare,使用外部变量执行):

$query = "INSERT INTO intervaltime(intervalnumber,destinationname,originname,distancevalue,durationintrafficvalue,durationintraffictext) VALUES(:intervalnumber,:destination_addresses,:origin_addresses,:distance,:duration_in_traffic_value,:duration_in_traffic_text)";

$pdoResult = $db->prepare($query);

$pdoExec = $pdoResult->execute(array(
    "intervalnumber" => $intervalnumber,
    "destination_addresses" => $destination_addresses,
    "origin_addresses" => $origin_addresses,
    "distance" => $distance,
    "duration_in_traffic_value" => $duration_in_traffic_value,
    "duration_in_traffic_text" => $duration_in_traffic_text
    ));

if ($pdoExec) {
    echo("data Inserted!");
} else {
    echo("Data not Inserted!");
}

另外,请注意使用以下引号:

echo("data Inserted!");

它应该工作:)

祝你有愉快的一天, 安德烈