PHP:循环不断从 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 中显示所有用户的用户名,而不是一个。

EG。 http://gdata.youtube.com/feeds/api/users/PewDiePie,MissFushi/uploads?max-results=13

但我需要它像

EG。 http://gdata.youtube.com/feeds/api/users/MissFushi/uploads?max-results=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 个视频。有什么想法吗?

您不需要 implode 来获取用户名。 直接做 for each 得到 array_filter($v) 结果:

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

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

$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 
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);

任何阅读并想知道它最终是如何工作的人?嗯,就在这里