Hello guys, i’m working on a code since few days , that is conneting to twitter API.
Everything is working fine, i’m able to connect, receive the json objects, decode and store it.
The problem comes when I want to insert some data in MySQL, that’s just so weird, i’m using a foreach loop to send each one of the ids retrieved by the object with an INSERT IGNORE to avoid duplicates, when it’s done, i go to check MySQL is everything was fine, but i’m seeing only 33 items (that are my followers ids) but i should have 66.
I made some changes to be able to analyse logs … and i’m seeing that an and id like " 247346494" is being insered the 33 other times instead of the real ones, so when i replace INSERT IGNORE by simply INSERT, it adds me 66 items as required, but there are 33 duplicates of an id that doesn’t even exist ! . So i’m wondering what went wronged : at each loop i’m inserting the item : $value->id, that is different each loop, ( i’m echoing it just befoire the insert, and just after , it should add the stuff in the echo, but instead it adds the id that doesnt event exist ,because for debugging purposes i displayed all that must be added, and the one that is being duplicated 33 times isn’t even in the object , neither in the values returned by $value->id, so i don’t know here does it come from, and how to make it work.
Here is the sample of my code that insert data ($b is the data received with the twitter API and it works well, as i’m able to display the 66 elements that i must insert with an echo just before and after the query)
<?php
error_reporting(E_ALL);
ini_set(‘display_errors’, 1);
require_once(‘TwitterAPIExchange.php’);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
function savefollowerstoDB($b, $usertocheckid)
{
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
foreach($b as $key => $value){
try{
$res=$conn->query("INSERT IGNORE INTO FollowersIDOfTheUserToCheck(usertocheck_id , follower_id) VALUES('$usertocheckid', '$value->id');") or die("Query Failed! SQL: ".$conn->error);
if($res == TRUE){
echo"added id : $value->id"; // just to see which value is being treated.
}
else{
echo($conn_error);
}
}catch(Exception $e){
echo($e->getMessage());
}
}
}