PHP:Loop保持从SQL检索所有用户,而不是一次只检索一个用户


PHP: Loop Keeps Retrieving All Users From SQL Instead Of Doing One At A Time

这里的概念是,数据库将youtube频道用户名存储在一个名为youtube的单元格中。

我需要代码来查找USERDB,查找YOUTUBE单元格,并检索存储在列表中的所有用户名(任何空白单元格都不会显示)。

从这里开始,我需要将用户youtube用户名从youtube单元格放入FEEDURL 的代码

我需要这个循环,这样它就可以根据结果对每个用户进行操作。我遇到的问题是,FEEDURL显示的是URL中的所有用户用户名,而不是一个。

例如。http://gdata.youtube.com/feeds/api/users/PewDiePie,富诗小姐/上传?最大结果=13

但我需要它像

例如。http://gdata.youtube.com/feeds/api/users/MissFushi/uploads?max-结果=13

这是我的代码

$communityvideos = mysql_query("SELECT youtube FROM userdb WHERE rights='user' && youtube IS NOT NULL");
while($youtube = mysql_fetch_array($communityvideos)) {
$v[] = $youtube["youtube"];
}
$youtube2 = implode(',', array_filter($v));
$usernames = array($youtube2);
error_reporting(E_ALL);
foreach ($usernames as $user) {
$feedURL = 'http://gdata.youtube.com/feeds/api/users/' . $user .'/uploads?max-results=13';
$sxml = simplexml_load_file($feedURL);
}
$i=0;
foreach ($sxml->entry as $entry) {
      $media = $entry->children('media', true);
      $watch = (string)$media->group->player->attributes()->url;
      $thumbnail = (string)$media->group->thumbnail[0]->attributes()->url;
parse_str( parse_url( $watch, PHP_URL_QUERY ), $my_array_of_vars);

最后,我只想总共播放13个视频。不是每个用户都有13个视频,而是所有加入的用户中只有13个视频。对此有什么想法吗?

尝试使用此代码。我对数组的操作方式进行了一些编辑。请确保开始使用mysqli_*扩展而不是mysql_*,因为前者更安全,而且不太容易注入SQL。

$communityvideos = mysql_query("SELECT youtube FROM userdb WHERE rights='user' && youtube IS NOT NULL");
$usernames = array();
while($youtube = mysql_fetch_assoc($communityvideos)) {
    $usernames[] = $youtube['youtube'];
}
//Redacted this does not do what you want it to do. This glues all usernames together and wrecks the $usernames array.
//$youtube2 = implode(',', array_filter($v));
//$usernames = array($youtube2);
error_reporting(E_ALL);
foreach ($usernames as $user){
$feedURL = 'http://gdata.youtube.com/feeds/api/users/' . $user .'/uploads?max-results=13';
$sxml = simplexml_load_file($feedURL);

// Place this inside the $usernames loop
$i=0;
foreach ($sxml->entry as $entry) {
      $media = $entry->children('media', true);
      $watch = (string)$media->group->player->attributes()->url;
      $thumbnail = (string)$media->group->thumbnail[0]->attributes()->url;
        parse_str( parse_url( $watch, PHP_URL_QUERY ), $my_array_of_vars);
        // And whatever came after this it doesn't show in the question.
    }
}
// You will have to figure the part below as well. Since you the last iteration of your foreach loop above will end with the last result in $sxml.
// The loop below will only loop the xml for the last result 

您不需要执行implode来获取用户名。只需使用array_filter($v)结果直接执行for each即可:

$users = array_filter($v);
foreach ($users as $user) {
$feedURL = 'http://gdata.youtube.com/feeds/api/users/' . $user .'/uploads?max-results=13';
$sxml = simplexml_load_file($feedURL);
}
ommunityvideos = mysql_query("SELECT * FROM userdb WHERE rights='user' && youtube IS NOT NULL");
while($youtube = mysql_fetch_assoc($communityvideos)) {
    $v[] = $youtube['youtube'];
error_reporting(E_ALL);
$usernames = array_filter($v);
foreach ($usernames as $user){
$feedURL = 'http://gdata.youtube.com/feeds/api/users/' . $user .'/uploads?max-results=13';
$sxml = simplexml_load_file($feedURL);
$i=0;
foreach ($sxml->entry as $entry) {
      $media = $entry->children('media', true);
      $watch = (string)$media->group->player->attributes()->url;
      $thumbnail = (string)$media->group->thumbnail[0]->attributes()->url;
        parse_str( parse_url( $watch, PHP_URL_QUERY ), $my_array_of_vars);

对于任何阅读并想知道它最终是如何工作的人来说?这里是