So, i'm trying to make a script to collect YouTube channel data. I want to be able to update the data with a click of a button. Right now, it's not going through the users in my database and updating the information when the update query is in the while loop. When I make it so the bracket at the very end after the update query is right after the while loop, it'll update one record in the database. I need it to be able to loop through my database and update all users with the rank partner. Yes all the columns and everything are right because it does update it when not in the while loop, but just one user and not all.
所以,我正在尝试制作一个脚本来收集YouTube频道数据。我希望能够通过单击按钮更新数据。现在,当更新查询在while循环中时,它不会通过我的数据库中的用户并更新信息。当我将更新查询后的括号放在while循环之后,它将更新数据库中的一条记录。我需要它能够遍历我的数据库并使用排名合作伙伴更新所有用户。是的所有列和一切都是正确的,因为它不是在while循环中更新它,而只是一个用户而不是全部。
$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();
}
1 个解决方案
#1
You're overwriting the $query
which you are looping over when you write the first user:
当你编写第一个用户时,你正在覆盖你正在循环的$查询:
$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();
Change $query
in this portion of the code to anything ($q
?), and it will do what you need it to do for all the users
将此部分代码中的$ query更改为任何内容($ q?),它将执行您需要它为所有用户执行的操作
#1
You're overwriting the $query
which you are looping over when you write the first user:
当你编写第一个用户时,你正在覆盖你正在循环的$查询:
$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();
Change $query
in this portion of the code to anything ($q
?), and it will do what you need it to do for all the users
将此部分代码中的$ query更改为任何内容($ q?),它将执行您需要它为所有用户执行的操作