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>';
php mysql json api twitch
|
show 3 more comments
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>';
php mysql json api twitch
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
|
show 3 more comments
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>';
php mysql json api twitch
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
php mysql json api twitch
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
|
show 3 more comments
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
|
show 3 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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