【问题标题】:PHP: Loop Keeps Retrieving All Users From SQL Instead Of Doing One At A TimePHP:循环不断从 SQL 中检索所有用户,而不是一次只检索一个用户
【发布时间】:2015-02-26 20:40:46
【问题描述】:

这里的概念是数据库将 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 个视频。有什么想法吗?

【问题讨论】:

  • 您正在以某种奇怪的方式编辑数组。让我想出一个可能的解决方案。

标签: php youtube youtube-api


【解决方案1】:

尝试使用此代码。我对数组的操作方式进行了一些编辑。确保开始使用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 

【讨论】:

  • 那行不通。它适用于列表中的最后一个用户,就像我删除你放入的所有内容时所预测的一样//。保留 $v[] =..... 并添加到 $users = array_filter($v);并且 foreach 成为用户作为用户。这很有效,它只显示列表中最后一个用户的视频,而不是其他用户,这将属于你大约是的第二部分?
  • 你的问题是你一直在重写$sxml我会编辑试试看。
  • 我将 $sxml 循环放在了用户名循环中。确保将其与之后的代码相加,因为它在您为问题提供的数据中不完整。
  • 是的,你是对的我已经修改了,现在循环正在工作我只需要弄清楚如何改变它,所以它只显示总共 13 个结果,因为现在有 2 个用户名操作它显示 26 个结果
  • 哦,你说你看不到下面的内容,在解析下面是边框链接等的 html 部分,然后当回显完成后,结尾是 $i++; if($i==5) { $i=0; } }
【解决方案2】:

您不需要使用implode 来获取用户名。 只需直接使用for eacharray_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);
}

【讨论】:

  • nope 开始使用您之前的答案 $users = array_filter($v);但只显示数组列表中最后一个人的视频。
  • 请删除array_filter。它没有必要。如果您仍然想使用它,请使用$users[] = array_filter($v); users 需要是一个数组。
  • 删除过滤器会导致错误,因为它使我的空白条目保持不变,添加过滤器并执行您所说的操作已更正了错误,仅显示已在其中输入用户名的用户。虽然它作为 $ users = array_filter($v) 而不是 $users[] = array_filter($v) 导致错误:/
【解决方案3】:
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);

对于任何阅读并想知道它最终如何修复的人?嗯,就在这里

【讨论】:

    猜你喜欢
    • 2014-11-16
    • 2021-10-10
    • 2017-01-18
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 2011-09-26
    相关资源
    最近更新 更多