[SOLVED] MySQL request : half working

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());
}
								
	}

}

What is the Unique key that should be triggering when you insert a new row? I would say the ID you’re retrieving is probably a primary auto inc column that has blown out due to the amount of rows in your table. If you want INSERT IGNORE to work correctly then you will need to add a unique key to the twitter ID column.
Run “SHOW CREATE TABLE FollowersIDOfTheUserToCheck” to view the table schema and ensure your indexes are correct.

Hey polk, thanks for your anwser.

ThE unique key is the couple (usertocheck_id, follower_id) , because , two differents user can be followed by the same person, and the usertocheck_id is never unique because he has many followers so its id is being insert as much as he has followers. But a user can’t be followed by the same person twice, so i’ve set the primary key to (usertocheck_id, follower_id), and there is no auto inc column as the ids are retrieved from the twitter API as a json object i decode. Further more, as i said , before inserting rows, i echo the “$value->id” and the value inserted is just totally different (it doesn’t even exist in my followers , neither of twitter website) .
So there are only 2 columns in the table : userotcheck_id , and follower_id that makes a primary key.

As we can see there are many ids being inserted when i launch the function :


But the last one 2147483647 is the one that doesn’t exist and make problems happen by being inserted 33 , if duplicates were accepted. Even if $value->id is containing the real ids that must be inserted

That value is the maximum for a signed 32 bit integer and is the result of values greater than that being converted to the maximum value for the column definition by the database engine (combined with the db server mode setting.)

Twitter IDs however are unsigned 64 bit integers, which it is suggested that you use the string field in the json data to avoid conversion errors (if you treat the value as an integer within your php code, which luckily you are not, they will be limited to php’s maximum integer value, which is tied to the processor register size and if php was built for 32 or 64 bits.)

You need to change your database table definition from INT to BIGINT for both columns holding twitter ids.

I would run “SHOW CREATE TABLE FollowersIDOfTheUserToCheck” anyway. It’s possible your column definitions are incorrect. It could be that the integers you are receiving are unsigned and you are have your table setup with signed integers.

Edit: What phdr said.

https://dev.mysql.com/doc/refman/8.0/en/integer-types.html

Wow ! Just thank you guys, that was so obvious and so “hard” to see at the same time ! What a nice and fast help , appreciate it !

Sponsor our Newsletter | Privacy Policy | Terms of Service