I have built a php script that saves results from a hash tag search and when I save the tweet ID it just tries saving a duplicate.
I have set a UNIQUE constraint on the table and it obviously doesn’t save anything but the first tweet and it throws the error below. Which isn’t surprising
ERROR: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2147483647' for key 2
If I remove the constraint it saves all the tweets correctly but the tweet ID is duplicated
I have tried displaying the tweet in the output from the same variable I’m using to put in the table and it is all UNIQUE and correct so the variable is correct. So I can only assume some weirdness is going on all by itself
foreach($results as $result) {
$tweet_ID = $result->id_str;
$userID = $result->from_user_id_str;
$tweetText = $result->text;
$tweet_time = strtotime($result->created_at);
$createdAt = date('Y-m-d H:i:s ',$tweet_time);
echo '<div>'. $tweet_ID .
'<div class="tweet" >' . displayTweet($result->text),"\r\n" .
'<div class="user">'. '<strong>Posted </strong>' . date('j/n/y H:i:s ',$tweet_time). '<strong> By </strong>' .
'<a rel="nofollow" href="http://twitter.com/' . $result->from_user. '">' . $result->from_user .
'</div>' .
'</div>';
// Execute query
$stmt = $conn->prepare("INSERT INTO ".$hashtag."(tweetID, userID, tweetText, createdAt) VALUES(:tweetID, :userID, :tweetText, :createdAt)");
$stmt->execute(array(':tweetID' => $tweet_ID, ':userID' => $userID, ':tweetText' => $tweetText, ':createdAt' => $createdAt));
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
I can’t figure out for the life of me why it is saving some random numbers
The number that it is adding is 2147483647. Doesn’t mean anything to me.
Any help on what is going on would be appreciated
This number
2147483647clearly indicates that you are not using 64-bit integers to store tweet id. Tweet ids are 64-bit integers. You should useBIGINTdata type fortweetIDcolumn.The special value you are getting
2147483647is because all the upper bits are truncated when a 64-bit tweet id is cast to 32-bitsigned int. If you could have useunsigned intThe number would have been higher. But not more than2147483647*2. Tweet ids are much higher than that. You have to use 64-bitunsigned intAlso I see you are using a dynamic table name for your insert query. Dont do that! One single table can hold a lot of rows before you experience performance problem.