Mysql compare json data with database









up vote
0
down vote

favorite












The idea behind the code below is to make a "Unfollower alert" for Twitch users/streamers.. The concept: Twitch api gives me a list of current followers of a channel which in my code if i find a new name I put him in the database under follower name and the same for follow time. My problem: I can`t get the sql code to understand and compare the current followers with the database and display the follower which is missing from the api but not in the database.



 <?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



part where it doesnt work



 $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>';












share|improve this question























  • At what step in your logic is it not working correctly?
    – Joseph_J
    Nov 11 at 2:14










  • I edited it sorry
    – Daniel Baychev
    Nov 11 at 2:16










  • So in the part that does not work. What are you trying to accomplish?
    – Joseph_J
    Nov 11 at 2:28










  • When I get all followers from a channel, I put them in the DB under follower_name. If someone stops following (Is in the database, but not in the API or JSON), I want to echo his name.. In this case a name : BaychevLive should appear, instead the first name appears as much times as there are names
    – Daniel Baychev
    Nov 11 at 2:33






  • 1




    Your logic here is messed up. Your else statement basically guarantees that the name is already on the DB, The query that you have there now will select all of the names on the DB except the one that you are testing. I am sorry, I am still confused. The $fnames WILL ALWAYS EXIST on the DB.
    – Joseph_J
    Nov 11 at 3:03














up vote
0
down vote

favorite












The idea behind the code below is to make a "Unfollower alert" for Twitch users/streamers.. The concept: Twitch api gives me a list of current followers of a channel which in my code if i find a new name I put him in the database under follower name and the same for follow time. My problem: I can`t get the sql code to understand and compare the current followers with the database and display the follower which is missing from the api but not in the database.



 <?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



part where it doesnt work



 $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>';












share|improve this question























  • At what step in your logic is it not working correctly?
    – Joseph_J
    Nov 11 at 2:14










  • I edited it sorry
    – Daniel Baychev
    Nov 11 at 2:16










  • So in the part that does not work. What are you trying to accomplish?
    – Joseph_J
    Nov 11 at 2:28










  • When I get all followers from a channel, I put them in the DB under follower_name. If someone stops following (Is in the database, but not in the API or JSON), I want to echo his name.. In this case a name : BaychevLive should appear, instead the first name appears as much times as there are names
    – Daniel Baychev
    Nov 11 at 2:33






  • 1




    Your logic here is messed up. Your else statement basically guarantees that the name is already on the DB, The query that you have there now will select all of the names on the DB except the one that you are testing. I am sorry, I am still confused. The $fnames WILL ALWAYS EXIST on the DB.
    – Joseph_J
    Nov 11 at 3:03












up vote
0
down vote

favorite









up vote
0
down vote

favorite











The idea behind the code below is to make a "Unfollower alert" for Twitch users/streamers.. The concept: Twitch api gives me a list of current followers of a channel which in my code if i find a new name I put him in the database under follower name and the same for follow time. My problem: I can`t get the sql code to understand and compare the current followers with the database and display the follower which is missing from the api but not in the database.



 <?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



part where it doesnt work



 $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>';












share|improve this question















The idea behind the code below is to make a "Unfollower alert" for Twitch users/streamers.. The concept: Twitch api gives me a list of current followers of a channel which in my code if i find a new name I put him in the database under follower name and the same for follow time. My problem: I can`t get the sql code to understand and compare the current followers with the database and display the follower which is missing from the api but not in the database.



 <?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



part where it doesnt work



 $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>';









php mysql json api twitch






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 8:49

























asked Nov 11 at 2:05









Daniel Baychev

227




227











  • At what step in your logic is it not working correctly?
    – Joseph_J
    Nov 11 at 2:14










  • I edited it sorry
    – Daniel Baychev
    Nov 11 at 2:16










  • So in the part that does not work. What are you trying to accomplish?
    – Joseph_J
    Nov 11 at 2:28










  • When I get all followers from a channel, I put them in the DB under follower_name. If someone stops following (Is in the database, but not in the API or JSON), I want to echo his name.. In this case a name : BaychevLive should appear, instead the first name appears as much times as there are names
    – Daniel Baychev
    Nov 11 at 2:33






  • 1




    Your logic here is messed up. Your else statement basically guarantees that the name is already on the DB, The query that you have there now will select all of the names on the DB except the one that you are testing. I am sorry, I am still confused. The $fnames WILL ALWAYS EXIST on the DB.
    – Joseph_J
    Nov 11 at 3:03
















  • At what step in your logic is it not working correctly?
    – Joseph_J
    Nov 11 at 2:14










  • I edited it sorry
    – Daniel Baychev
    Nov 11 at 2:16










  • So in the part that does not work. What are you trying to accomplish?
    – Joseph_J
    Nov 11 at 2:28










  • When I get all followers from a channel, I put them in the DB under follower_name. If someone stops following (Is in the database, but not in the API or JSON), I want to echo his name.. In this case a name : BaychevLive should appear, instead the first name appears as much times as there are names
    – Daniel Baychev
    Nov 11 at 2:33






  • 1




    Your logic here is messed up. Your else statement basically guarantees that the name is already on the DB, The query that you have there now will select all of the names on the DB except the one that you are testing. I am sorry, I am still confused. The $fnames WILL ALWAYS EXIST on the DB.
    – Joseph_J
    Nov 11 at 3:03















At what step in your logic is it not working correctly?
– Joseph_J
Nov 11 at 2:14




At what step in your logic is it not working correctly?
– Joseph_J
Nov 11 at 2:14












I edited it sorry
– Daniel Baychev
Nov 11 at 2:16




I edited it sorry
– Daniel Baychev
Nov 11 at 2:16












So in the part that does not work. What are you trying to accomplish?
– Joseph_J
Nov 11 at 2:28




So in the part that does not work. What are you trying to accomplish?
– Joseph_J
Nov 11 at 2:28












When I get all followers from a channel, I put them in the DB under follower_name. If someone stops following (Is in the database, but not in the API or JSON), I want to echo his name.. In this case a name : BaychevLive should appear, instead the first name appears as much times as there are names
– Daniel Baychev
Nov 11 at 2:33




When I get all followers from a channel, I put them in the DB under follower_name. If someone stops following (Is in the database, but not in the API or JSON), I want to echo his name.. In this case a name : BaychevLive should appear, instead the first name appears as much times as there are names
– Daniel Baychev
Nov 11 at 2:33




1




1




Your logic here is messed up. Your else statement basically guarantees that the name is already on the DB, The query that you have there now will select all of the names on the DB except the one that you are testing. I am sorry, I am still confused. The $fnames WILL ALWAYS EXIST on the DB.
– Joseph_J
Nov 11 at 3:03




Your logic here is messed up. Your else statement basically guarantees that the name is already on the DB, The query that you have there now will select all of the names on the DB except the one that you are testing. I am sorry, I am still confused. The $fnames WILL ALWAYS EXIST on the DB.
– Joseph_J
Nov 11 at 3:03

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245227%2fmysql-compare-json-data-with-database%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245227%2fmysql-compare-json-data-with-database%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto