[英]import json to mysql
我有這個json文件
{
"5927803e892d4c459f2992a3a1ac2ead.16": {
"lat": 57.532696,
"lng": 9.949331
},
"e25fe65e4fe4468db1361213ba1a376b.16": {
"lat": 57.501783,
"lng": 9.947415,
"name": "Vidstrup Train Station",
"disabled": false
},
"dcd8b44bd0f3478cb2d99c8d5115ba50.16": {
"lat": 57.480323,
"lng": 9.968769,
"name": "Vellingshøj Station",
"disabled": false
}
}
我有一個工作的PHP文件來導入它但我真的想要ID也是我的問題是在json文件中沒有像“id”這樣的名稱:“”
<?php
//
$connect = mysqli_connect("localhost", "xxxxx", "xxxxxx", "Rails");
$filename = "location-export.json";
$data = file_get_contents($filename);
$array = json_decode($data, true);
foreach($array as $row)
{
$sql = "INSERT INTO location(location_lat, location_lng, location_name, location_mission) VALUES ('".$row["lat"]."', '".$row["lng"]."','".$row["name"]."', '".$row["mission"]."')";
mysqli_query($connect, $sql);
}
echo "Data Inserted";
?>
更改您的代碼如下
foreach($array as $id => $row)
{
$sql = "INSERT INTO location(id, location_lat, location_lng, location_name, location_mission) VALUES ('{$id}','".$row["lat"]."', '".$row["lng"]."','".$row["name"]."', '".$row["mission"]."')";
mysqli_query($connect, $sql);
}
echo "Data Inserted";
?>
但是當然這比使用預備語句完全不安全(並且可能更慢)。
你應該試試
$stmt = mysql_prepare($link, "INSERT INTO location(id, location_lat, location_lng, location_name, location_mission) VALUES(?,?,?,?)");
foreach($array as $id => $row)
{
mysqli_stmt_bind_param($stmt,"ssss", $id, $row["lat"], ....);
mysqli_stmt_execute($stmt);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.