Advertisement
Guest User

Untitled

a guest
Mar 13th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. <?php
  2. //connect to mysql db
  3. $username = "test";
  4. $password = "test";
  5. $database = "wordpressdb";
  6. $host = "localhost";
  7.  
  8. $conn = mysqli_connect($host, $username, $password, $database)
  9. if($conn === false){
  10. die("FAIL" . mysqli_connect_error());
  11. }
  12.  
  13. //read the json file contents
  14. $jsondata = file_get_contents('test.json');
  15.  
  16.  
  17. //{"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"}]}]}
  18.  
  19.  
  20.  
  21.  
  22. //convert json object to php associative array
  23. $data = json_decode($jsondata, true);
  24.  
  25. //get the employee details
  26.  
  27.  
  28.  
  29.  
  30. $cod = $data['cod'];
  31. $calctime = $data['calctime'];
  32. $cnt = $data['cnt'];
  33. $id = $data['list']['id'];
  34. $name = $data['list']['name'];
  35. $lon = $data['list']['coord']['lon'];
  36. $lat = $data['list']['coord']['lat'];
  37. $temp = $data['list']['main']['temp'];
  38. $min = $data['list']['main']['temp_min'];
  39. $max = $data['list']['main']['temp_max'];
  40. $pressure = $data['list']['main']['pressure'];
  41. $level = $data['list']['main']['sea_level'];
  42. $level2= $data['list']['main']['grnd_level'];
  43. $humidity = $data['list']['main']['humidity'];
  44. $dt = $data['list']['dt'];
  45. $speed = $data['list']['wind']['speed'];
  46. $deg = $data['list']['wind']['deg'];
  47. $h = $data['list']['rain']['3h'];
  48. $all = $data['list']['clouds']['all'];
  49. $id2 = $data['list']['weather']['id'];
  50. $main = $data['list']['weather']['main'];
  51. $description = $data['list']['weather']['description'];
  52. $icon = $data['list']['weather']['icon'];
  53.  
  54. //insert into mysql table
  55. $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)
  56. 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')";
  57. if(mysqli_query($link, $sql)){
  58.  
  59. echo "Records inserted successfully.";
  60.  
  61. } else{
  62.  
  63. echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
  64.  
  65. }
  66. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement