Mysql将json数据与数据库进行比较

时间:2018-11-11 02:05:36

标签: php mysql json api twitch

下面的代码背后的想法是为Twitch用户/直播者创建“取消关注者警报”。概念:Twitch api给了我一个频道的当前关注者列表,如果我找到一个新名称,该频道会在我的代码中将他放在跟随者名称下的数据库中,跟进时间也一样。我的问题:我无法获取sql代码来理解当前的关注者并将其与数据库进行比较,并显示api中缺少但数据库中没有的关注者。

 <?php
 $connection = new mysqli("localhost","root","","twitchun");
 $channelsApi = 'https://api.twitch.tv/helix/users/follows? 
 to_id=176714165&first=10';
 $channelName = '';
 $clientId = 'pm7oxir9hxzhrl0hnzdxdrg';
 $ch = curl_init();
 curl_setopt_array($ch, array(
 CURLOPT_HTTPHEADER => array(
   'Client-ID: ' . $clientId
 ),
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_URL => $channelsApi . $channelName
 ));
 $response = curl_exec($ch);
 curl_close($ch);
 //echo $response;
 $json = json_decode($response, true);
 foreach ($json['data'] as $names){

  echo '<p>'.$names['from_name'].'</p>';
}



 foreach ($json['data'] as $names){
 $result = $connection->query("SELECT follower_name FROM followers WHERE 
 follower_name = '".$names['from_name']."'");
 if($result->num_rows == 0) {
 //if follower doesnt exist in database put him in
  mysqli_query($connection, 'INSERT into followers(follower_name, 
  follow_time) VALUES ("'.$names['from_name'].'", 
 "'.$names['followed_at'].'")'); 
 } else {
 //if follower exists in database and in json
 }

无法正常工作的部分

 $fnames= $names['from_name'];
 $result2 = $connection->query("SELECT follower_name FROM followers WHERE 
 follower_name <> '".$fnames."' order by follow_time desc");

   $unfollow = mysqli_fetch_assoc($result2);
   foreach($unfollow as $unfollower){
       echo '<p style="color:red">'.$unfollower.'</p>';
   }

 }

0 个答案:

没有答案