Bradly Spicer
Bradly Spicer

Reputation: 2318

Passing mysql query into a while loop

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

Upvotes: 0

Views: 43

Answers (1)

Nithin
Nithin

Reputation: 5840

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

Upvotes: 2

Related Questions