【发布时间】:2018-03-21 15:53:37
【问题描述】:
这是我的 JSON 数据,我需要将这些数据插入到我的数据库中。然后每5分钟,我会用setinterval检查这个JSON文件中是否有新添加的电台,如果有变化,应该只添加新的。但是我遇到了问题,所有字段都被再次添加。我不需要重复数据,我只想添加新的更新电台。
[
{
"stationname": "Radio Plus, Mauritius",
"websiteurl": "www.radioplus.mu",
"streamurl": "http://s4.voscast.com:8432/;",
"genre":"POP (Popular Music)",
"language":"French",
"country": "Mauritius",
"description":"Best Music"
},
{
"stationname": "Radio One",
"websiteurl": "r1.mu",
"streamurl": "http://151.80.44.127:8020/stream?type=http&nocache",
"genre":"POP (Popular Music)",
"language":"French",
"country": "Mauritius",
"description":"24/7 Music Everyday"
},
{
"stationname": "2RRR FM",
"websiteurl": "www.2rrr.org",
"streamurl": "http://110.142.218.7:88/broadwave.m3u?type=.mp3/;s",
"genre":"Electronic",
"language":"English",
"country": "Austria",
"description":"Good Music all day and night"
},
{
"stationname": "22G Radio",
"websiteurl": "www.22gradio.com",
"streamurl": "http://s6.voscast.com:9224/;stream.mp3",
"genre":"Electronic",
"language":"English",
"country": "India",
"description":"Good Music in India"
},
{
"stationname": "80’s / 90’s Radio",
"websiteurl": "hitconnection.fr",
"streamurl": "http://streaming.radionomy.com/80-s-90-s-HitConnectionRadio",
"genre":"Electronic",
"language":"French",
"country": "French",
"description":"This online radio station provides twenty four hour (24h) mix of 80’s / 90’s of music. The station plays all the 80’s / 90’s music you love while striving to keep up with the desires of its listeners."
},
{
"stationname": "FM Reggae Trade Radio",
"websiteurl": "www.1.fm",
"streamurl": "http://sc1c-sjc.1.fm:7078/;",
"genre":"Raggae",
"language":"Other",
"country": "Brazil",
"description":"Entertainment, Fun and excitement are the specialty of 1.FM Reggae Trade Radio the radio is always live and lively with its energetic presentation, promotion of music and programs. Its a complete package as a full entertainment rich radio broadcasting from Brazil With 1.FM Reggae Trade Radio you can enjoy lots of class leading musical programs."
},
{
"stationname": "01 Internet of Music",
"websiteurl": "www.internetofmusic.com",
"streamurl": "http://radio.internetofmusic.com:8000/live.nsv",
"genre":"World Music / Beats",
"language":"English",
"country": "United States of America",
"description":"01 Internet of Music is all about good music as they believes “Music in the Answer”. By tuning in to 01 Internet of Music listeners from around the world can enjoy the amazingly entertaining musical programs presented by the talented broadcasting team of 01 Internet of Music."
},
{
"stationname": "I love Kpop",
"websiteurl": "www.ilovekpop.wifeo.com",
"streamurl": "http://streaming.radionomy.com/IloveKpop?type=.mp3",
"genre":"R&B / Soul",
"language":"English",
"country": "Korea North",
"description":"In Korea there are lots of pop music based radio station available through out the whole country and I love Kpop is one of those popular radios broadcasting live from there. Unlike other radio stations of Korea this radio is always known best for broadcasting pop music along with musical information."
},
{
"stationname": "2cr Radio",
"websiteurl": "www.2cr.com.au",
"streamurl": "http://s6.voscast.com:8016/;",
"genre":"Pop (Popular music)",
"language":"Japanese",
"country": "China",
"description":"2cr Radio is a very popular radio on China. They play Chines music, it is a 24 hrs online radio. Enjoy 2cr Radio."
},
{
"stationname": "Akash",
"websiteurl": "www.2cr.com.au",
"streamurl": "http://s6.voscast.com:8016/;",
"genre":"Pop (Popular music)",
"language":"Japanese",
"country": "China",
"description":"2cr Radio is a very popular radio on China. They play Chines music, it is a 24 hrs online radio. Enjoy 2cr Radio."
}
]
这是我的添加代码:
<?php
$db = mysqli_connect("localhost","root","","registration");
$filename = "station.json";
$data = file_get_contents($filename);
$array = json_decode($data,true);
foreach($array as $roww){
$stationname = $roww['stationname'];
$sql_name = "SELECT * FROM radiotest WHERE stationname='$stationname'";
$res_name = mysqli_query($db, $sql_name);
}
foreach ($array as $row) {
if (mysqli_num_rows($res_name) > 0) {
return false;
} else{
$sql = "INSERT INTO radiotest(stationname,websiteurl,streamurl,genre,language,country,description)
VALUES ('".$row["stationname"]."', '".$row["websiteurl"]."', '".$row["streamurl"]."', '".$row["genre"]."', '".$row["language"]."', '".$row["country"]."', '".$row["description"]."')";
mysqli_query($db,$sql);
}
}
?>
有人可以帮助我吗?如何避免这种情况? 谢谢!
【问题讨论】: