doudui2229 2015-02-19 15:45
浏览 39

将mysql查询传递给while循环

The error I'm getting is

Warning: Variable passed to each() is not an array or object in /home2/xtrapsp/public_html/Admin/index.php on line "while (list(, $val) = each($channel)) { "

However, I am using a direct object within the each() parameters.

$query = "SELECT * FROM Streamers" or die("Error in the consult.." . mysqli_error($link));
$row = mysqli_fetch_array($result);
$channel = $row["name"];
//execute the query. 
$result = mysqli_query($link, $query); 

    while (list(, $val) = each($channel)) {
       $url = "https://api.twitch.tv/kraken/streams/".$val;
       $json = file_get_contents($url);
       $json = json_decode($json);
       $stream =  $json->stream;
             if($stream != null){   
                    $channelAPI = json_decode(file_get_contents('https://api.twitch.tv/kraken/channels/'. $val));
                    $status     = $channelAPI->status;
                    $name       =  $channelAPI->display_name;
                    $gameimg    = "http://static-cdn.jtvnw.net/ttv-boxart/".$channelAPI->game . "-272x380.jpg";
                    $viewers    = $streamsAPI->stream->viewers;
                    $followers  = $channelAPI->followers;
                    $views      = $channelAPI->views;
                    $avatar     = $channelAPI->logo;

                        echo    '<tr><td><a href="/cast.php?caster='.$val.'"/><img src="' . $avatar . '" width="60px"/></a></td>';
                        echo    '<td><a href="#"> <i class="fa fa-circle text-success"></i> Online</a>  </td>';
                        echo    '<td>Game: '. $channelAPI->game.'</tr>';            
            }
        }
        if($stream == null){    
            Echo    'No Dream2Streamers online!';
        }

Would someone mind explaining why it is throwing this error? I am trying to query if the channel inside a mysql database is online and if it is, produce the right data on page.

Thanks

  • 写回答

1条回答 默认 最新

  • doukuang1950 2015-02-19 15:49
    关注

    i think its cause you are fetching the date before running the query , try this

    $query = "SELECT * FROM Streamers" or die("Error in the consult.." . mysqli_error($link));
    //execute the query. 
    $result = mysqli_query($link, $query);
    
    while ($row =  mysqli_fetch_array($result)) {
       $val = $row["name"];
       $url = "https://api.twitch.tv/kraken/streams/".$val;
       $json = file_get_contents($url);
       $json = json_decode($json);
       $stream =  $json->stream;
             if($stream != null){   
                    $channelAPI = json_decode(file_get_contents('https://api.twitch.tv/kraken/channels/'. $val));
                    $status     = $channelAPI->status;
                    $name       =  $channelAPI->display_name;
                    $gameimg    = "http://static-cdn.jtvnw.net/ttv-boxart/".$channelAPI->game . "-272x380.jpg";
                    $viewers    = $streamsAPI->stream->viewers;
                    $followers  = $channelAPI->followers;
                    $views      = $channelAPI->views;
                    $avatar     = $channelAPI->logo;
    
                        echo    '<tr><td><a href="/cast.php?caster='.$val.'"/><img src="' . $avatar . '" width="60px"/></a></td>';
                        echo    '<td><a href="#"> <i class="fa fa-circle text-success"></i> Online</a>  </td>';
                        echo    '<td>Game: '. $channelAPI->game.'</tr>';            
            }
        }
        if($stream == null){    
            Echo    'No Dream2Streamers online!';
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备