gpt4 book ai didi

php - 数据未从 JSON 插入数据库

转载 作者:行者123 更新时间:2023-11-29 18:03:43 25 4
gpt4 key购买 nike

我正在将数据从 Json 存储到数据库。一些行[]元素[]正在插入,但这是一个随机调用。每次刷新后,仅存储第一个 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 = ("插入间隔时间(database_parameters) VALUES('$intervalnumber','$destination_addresses','$origin_addresses','$distance','$duration_in_traffic_value','$duration_in_traffic_text')"); $pdoResult = $db->准备($query); $pdoExec = $pdoResult->execute(); 如果($pdoExec){ echo('数据已插入!'); } 别的 { echo('数据未插入!'); } //关闭数据库连接 回显“
”; 回显“
”;
休息;}} $db = 空; ?>

最佳答案

您能否尝试这样的请求(使用准备,使用外部变量执行):

$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!");

应该可以:)

祝你有美好的一天,安德烈

关于php - 数据未从 JSON 插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48103375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com