Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 更新包含转发计数的MySQL行(如果已转发)_Php_Mysql_Twitter_On Duplicate Key - Fatal编程技术网

Php 更新包含转发计数的MySQL行(如果已转发)

Php 更新包含转发计数的MySQL行(如果已转发),php,mysql,twitter,on-duplicate-key,Php,Mysql,Twitter,On Duplicate Key,我一直在使用Twitter API和PHP使用以下函数将tweet记录到MySQL数据库中: function saveTweets($screen_name) { global $link; $screen_name = dbEscape(strtolower(trim($screen_name))); if (!$screen_name) { echo "<p><strong>Error: No screen name declared.</strong&g

我一直在使用Twitter API和PHP使用以下函数将tweet记录到MySQL数据库中:

 function saveTweets($screen_name) {
global $link;

$screen_name = dbEscape(strtolower(trim($screen_name)));
if (!$screen_name) { echo "<p><strong>Error: No screen name declared.</strong></p>\n"; return false; }

$row = dbGetRow("SELECT `id` FROM `twitter` WHERE `screen_name`='$screen_name' ORDER BY `id` DESC LIMIT 1");
$last_id = $row['id'];


$url = "http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=$screen_name&count=1500&include_rts=true" ;
if ($last_id) { $url .= "&since_id=$last_id" ; }
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
$xml = curl_exec ($ch);
curl_close ($ch);

$affected = 0;
$twelement = new SimpleXMLElement($xml);
foreach ($twelement->status as $status) {
    $text = dbEscape(trim($status->text));
    $time = strtotime($status->created_at);
    $id = $status->id;
    $retweet_count = $status->retweet_count;
 dbQuery("INSERT INTO `twitter`        (`id`,`screen_name`,`time`,`text`,`hidden`,`retweet_count`) VALUES     ('$id','$screen_name','$time','$text','n','$retweet_count') ON DUPLICATE KEY UPDATE $row[retweet_count] = VALUES('$retweet_count')");
    $affected = $affected + dbAffectedRows();
}

return "<p>".number_format($affected)." new tweets from $screen_name saved.</p>\n" ;
}

 echo saveTweets('Phil');
?>
这样,当我稍后运行查询时,如果tweet被转发,它会更新行

retweet_count的大多数条目都是相同的(0或1),因此我不能将retweet_count设置为唯一,尽管主键-id-应该是唯一的,并且我还有一个自动递增的列id-colid-尽管它实际上不是从twitter api中提取的,所以它可以是唯一的,并且被设置为唯一的


是否有更好的函数来更新已经包含int的行?谢谢

您的语法不正确。应该是:

ON DUPLICATE KEY UPDATE column_name = new_value, ...
即:

ON DUPLICATE KEY UPDATE `retweet_count` = '$retweet_count'
或者,如果您确实想使用该函数(在您的情况下并不特别有用-它只在使用单个
INSERT
语句插入多行时才有用):


虽然我同意这可能是我的语法,并感谢您这么快回复,但不幸的是,这些建议没有更新数据库。VALUES()函数只是我在这里的另一篇文章中找到的一个例子,所以我不必为使用它而大惊小怪。
ON DUPLICATE KEY UPDATE `retweet_count` = '$retweet_count'
ON DUPLICATE KEY UPDATE `retweet_count` = VALUES(`retweet_count`)