将mysql查询传递到while循环中


Passing mysql query into a while loop

我得到的错误是

警告:传递给each()的变量不是"while(list(,$val)=each($channel)"行/home2/xtrapp/public_html/Admin/index.php中的数组或对象{"

但是,我在each()参数中使用了一个直接对象。

$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!';
        }

有人能解释一下为什么会出现这个错误吗?我试图查询mysql数据库中的通道是否在线,如果在线,则在页面上生成正确的数据。

感谢

我认为这是因为你在运行查询之前获取了日期,请尝试这个

$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!';
    }