簡體   English   中英

嘗試讀取json並使用php在sql中寫入

[英]Trying to read json and write it in sql with php

我試圖從文件或URL中讀取JSON,然后使用PHP在SQL中寫入

我做了這段代碼:

<?php
    //connect to mysql db
$username = "test";
$password = "test";
$database = "wordpressdb";
$host = "localhost";

$conn = mysqli_connect($host, $username, $password, $database)
    if($conn === false){
        die("FAIL" . mysqli_connect_error());
    }

    //read the json file contents
    $jsondata = file_get_contents('test.json');


//{"cod":"200","calctime":0.3107,"cnt":15,"list":[{"id":2208791,"name":"Yafran","coord":{"lon":12.52859,"lat":32.06329},"main":{"temp":9.68,"temp_min":9.681,"temp_max":9.681,"pressure":961.02,"sea_level":1036.82,"grnd_level":961.02,"humidity":85},"dt":1485784982,"wind":{"speed":3.96,"deg":356.5},"rain":{"3h":0.255},"clouds":{"all":88},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}]}]}




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

    //get the employee details




    $cod = $data['cod'];
    $calctime = $data['calctime'];
    $cnt = $data['cnt'];
    $id = $data['list']['id'];
    $name = $data['list']['name'];
    $lon = $data['list']['coord']['lon'];
    $lat = $data['list']['coord']['lat'];
    $temp = $data['list']['main']['temp'];
    $min = $data['list']['main']['temp_min'];
    $max = $data['list']['main']['temp_max'];
    $pressure = $data['list']['main']['pressure'];
    $level = $data['list']['main']['sea_level'];
    $level2= $data['list']['main']['grnd_level'];
    $humidity = $data['list']['main']['humidity'];
    $dt = $data['list']['dt'];
    $speed = $data['list']['wind']['speed'];
    $deg = $data['list']['wind']['deg'];
    $h = $data['list']['rain']['3h'];
    $all = $data['list']['clouds']['all'];
    $id2 = $data['list']['weather']['id'];
    $main = $data['list']['weather']['main'];
    $description = $data['list']['weather']['description'];
    $icon = $data['list']['weather']['icon'];

    //insert into mysql table
    $sql = "INSERT INTO test(cod, calctime, cnt, id, name, lon, lat, temp, temp_min, temp_max, pressure, sea_level, grnd_level, humidity, dt, speed, deg, 3h, all, id, main, descriptio, icon)
    VALUES('$cod', '$calctime', '$cnt', '$id', '$name', '$lon', '$lat', '$temp', '$min', '$max', '$pressure', '$level', '$level2', '$humidity', '$dt', '$speed', '$deg', '$h', '$deg', '$all', '$id2', '$main', '$description', '$icon')";
   if(mysqli_query($link, $sql)){

    echo "Records inserted successfully.";

} else{

    echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);

}
?>

但是代碼不起作用:(我是編程新手,我想知道如何制作此代碼(從Json到php到mysql)。有人可以幫助我修復代碼嗎?我正試圖從5天開始修復它。

$link未定義。 在您的情況下應該是$conn

  <?php
//connect to mysql db
 $username = "test";
 $password = "test";
 $database = "wordpressdb";
 $host = "localhost";

$conn = mysqli_connect($host, $username, $password, $database);
 if($conn === false){
    die("FAIL" . mysqli_connect_error());
}

//read the json file contents
$jsondata = file_get_contents('test.json');



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

//get the employee details

 $cod = $data['cod'];
 $calctime = $data['calctime'];
 $cnt = $data['cnt'];
 $id = $data['list'][0]['id'];
 $name = $data['list'][0]['name'];
 $lon = $data['list'][0]['coord']['lon'];
 $lat = $data['list'][0]['coord']['lat'];
 $temp = $data['list'][0]['main']['temp'];
 $min = $data['list'][0]['main']['temp_min'];
 $max = $data['list'][0]['main']['temp_max'];
 $pressure = $data['list'][0]['main']['pressure'];
 $level = $data['list'][0]['main']['sea_level'];
 $level2= $data['list'][0]['main']['grnd_level'];
 $humidity = $data['list'][0]['main']['humidity'];
 $dt = $data['list'][0]['dt'];
 $speed = $data['list'][0]['wind']['speed'];
 $deg = $data['list'][0]['wind']['deg'];
 $h = $data['list'][0]['rain']['3h'];
 $all = $data['list'][0]['clouds']['all'];
 $id2 = $data['list'][0]['weather'][0]['id'];
 $main = $data['list'][0]['weather'][0]['main'];
 $description = $data['list'][0]['weather'][0]['description'];
 $icon = $data['list'][0]['weather'][0]['icon'];

  //insert into mysql table
  $sql = "INSERT INTO  `test`
          (
          `cod`,`calctime`,`cnt`,`master_id`,
          `name`,`lon`,`lat`,`temp`,`temp_min`,
          `temp_max`,`pressure`,`sea_level`,`grnd_level`,
          `humidity`,`dt`,`speed`,`deg`,`3h`,`all`,`id`,
          `main`,`descriptio`,`icon`
          )
        VALUES(
               '$cod', '$calctime', '$cnt', '$id', 
               '$name', '$lon', '$lat', '$temp', '$min',
               '$max', '$pressure', '$level', '$level2',
               '$humidity', '$dt', '$speed', '$deg', '$h',
               '$all', '$id2', '$main', '$description', '$icon'
              )";
   if(mysqli_query($conn, $sql)){
    echo "Records inserted successfully.";
   }else{
      echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
     }
?>

您正在使用$ link變量,您必須使用$ conn var,而您忘記了要插入24個值的連接行中的分號,並且該表只有23個列,您不能重命名兩個具有相同名稱的列,請更改數據庫表中master_id的第一個ID

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM