0

我已经构建了一个 php 脚本来保存哈希标记搜索的结果,当我保存推文 ID 时,它只是尝试保存一个副本。我在表上设置了一个 UNIQUE 约束,它显然只保存了第一条推文,并抛出了下面的错误。这并不奇怪

ERROR: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2147483647' for key 2

如果我删除约束,它会正确保存所有推文,但推文 ID 是重复的

我尝试在输出中显示来自我用来放入表中的同一变量的推文,它都是唯一且正确的,因此变量是正确的。所以我只能假设一些奇怪的事情本身就在发生

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

我无法弄清楚为什么它会保存一些随机数

它添加的数字是 2147483647。对我来说没有任何意义。任何有关正在发生的事情的帮助将不胜感激

4

1 回答 1

2

这个数字2147483647 清楚地表明您没有使用64 位整数来存储推文 ID。推文 ID 是64 位整数。您应该使用列BIGINT的数据类型tweetID

`tweetID` BIGINT UNSIGNED

您获得的特殊值是因为当64 位推文 id 转换为32-bit2147483647时,所有高位都被截断。如果你可以使用这个数字会更高。但不超过。推文ID远高于此。您必须使用 64 位 signed intunsigned int2147483647*2unsigned int

我还看到您正在为插入查询使用动态表名。不要那样做!在遇到性能问题之前,一张表可以容纳很多行。

于 2012-12-19T23:13:29.013 回答