· 7 years ago · Nov 30, 2018, 01:34 PM
1<?php
2$con = mysqli_connect("localhost","root","","globe");
3
4// Check connection
5if (mysqli_connect_errno())
6{
7 echo "Failed to connect to MySQL: " . mysqli_connect_error();
8}
9
10$result = mysqli_query($con,"SELECT * FROM `radios`");
11if(mysqli_num_rows($result) == 0)
12{
13 //creation des tables
14$query ="DROP TABLE IF EXISTS `radios`;
15CREATE TABLE IF NOT EXISTS `radios` (
16 `id` varchar(80) NOT NULL,
17 `name` varchar(80) NOT NULL,
18 `lien` varchar(100) NOT NULL,
19 `id_ville` varchar(100) NOT NULL,
20 PRIMARY KEY (`id`)
21) ENGINE=MyISAM DEFAULT CHARSET=latin1;"
22mysqli_query($con,$query);
23$query = "DROP TABLE IF EXISTS `villes`;
24CREATE TABLE IF NOT EXISTS `villes` (
25 `id` varchar(100) NOT NULL,
26 `name` varchar(100) NOT NULL,
27 `geo_lat` float NOT NULL,
28 `geo_long` float NOT NULL,
29 `countryName` varchar(100) NOT NULL,
30 `countryCode` varchar(5) NOT NULL,
31 `utc` float NOT NULL,
32 PRIMARY KEY (`id`)
33) ENGINE=MyISAM DEFAULT CHARSET=latin1;"
34mysqli_query($con,$query);
35//lecture du JSON
36$json = file_get_contents("database.json");
37$obj = json_decode($json);
38//remplissage de la BD
39foreach($obj->villes as $ville)
40{
41 $query = "INSERT INTO `villes` (id, name, geo_lat, geo_long, countryName, countryCode, utc)\n";
42 $query .= "VALUES ('$ville->id','$ville->name','".$ville->geo[0]."','".$ville->geo[1]."','$ville->countryName','$ville->countryCode','$ville->utc');";
43 mysqli_query($con,$query);
44
45 foreach($ville->channels as $radio)
46 {
47 if($radio->lienFlux != "")
48 {
49 $query = "INSERT INTO `radios` (id, name, lien, id_ville)\n";
50 $query .= "VALUES ('$radio->id','$radio->name','$radio->lienFlux','$ville->id');";
51 mysqli_query($con,$query);
52 }
53 }
54}
55}
56?>