mysql比较json数据与数据库

sshcrbum  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(295)

下面代码背后的想法是为twitch用户/拖缆发出“unfollower警报”。。这个概念:TwitchAPI给了我一个频道当前追随者的列表,在我的代码中,如果我找到一个新的名字,我会把他放在数据库中follower name下,follow time也是这样。我的问题是:我无法让sql代码理解当前的follower并将其与数据库进行比较,也无法显示api中缺少但数据库中没有的follower。

<?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>';
   }

 }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题