因此,我試圖製作一個腳本來收集YouTube頻道數據。我希望能夠通過點擊一個按鈕來更新數據。現在,它不會通過我的數據庫中的用戶並在更新查詢處於while循環時更新信息。當我這樣做時,更新查詢後的括號在while循環之後,它將更新數據庫中的一條記錄。我需要它能夠循環訪問我的數據庫並使用排名合作伙伴更新所有用戶。是的,所有的列和一切都是正確的,因爲它不會在while循環中更新它,而只是一個用戶而不是全部。PHP PDO更新查詢不在選擇查詢內更新
$query = $db->prepare("SELECT Username FROM Users WHERE Rank = 'Partner'");
$query->execute();
while ($row = $query->fetch(PDO::FETCH_ASSOC)){
$channel = $row['Username'];
function findviews($channel) {
error_reporting(E_ALL^E_NOTICE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
curl_setopt($ch, CURLOPT_URL, 'http://socialblade.com/youtube/user/' . $channel);
$gdatapage = curl_exec($ch);
$gdatapage = strip_tags($gdatapage);
$getviews = explode("Views for the Last 30 Days:",$gdatapage);
$getviews = preg_replace("/\([^)]+\)/","",$getviews[1]);
$getviews = str_replace(",", "", trim($getviews));
$getviews = explode(" S",$getviews);
$getviews = str_replace(" ", "", trim($getviews[0]));
curl_close($ch);
return $getviews;
}
$views = findviews($channel);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/users/' . $channel);
$gdatapage = curl_exec($ch);
preg_match("/subscriberCount=\'([^\']*)\'/", $gdatapage, $subscribers);
curl_close($ch);
$subs = $subscribers[1];
$query = $db->prepare("UPDATE Users SET `Views` = :views, `Subs` = :subs WHERE `Username` = :channel");
$query->bindParam(':views', $views);
$query->bindParam(':subs', $subs);
$query->bindParam(':channel', $channel);
$query->execute();
}
是你綁定所有字符串的值嗎? – EternalHour
在一段時間內定義一個函數?定義循環外部,否則你正在不斷地定義該函數。 – Devon