Jump to content

PHP, Mysqli, and Twitch API Somethings not working right.


rivacom

Recommended Posts

So in a nutshell, what I'm trying to do is query our website, grab players Twitch usernames, and scan them through the Twitch API to see if their online or not.  If they are, it takes their usernames and viewer count and stores them in a new table.  This will be setup to run every 5 minutes.  However...  with 1 user it runs great, with more then one... I get the same viewer count for each user.  Plus it's taking offline users as well.  Any advice would be great! Thanks.

<?php

// Upload to Database
$con=mysqli_connect("localhost","username","password","database");


// Check connection
if (mysqli_connect_errno())
 {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

$deletetable = $con->prepare('TRUNCATE TABLE twitch_streams');
$deletetable->execute();
$deletetable->close();

if ($result = $con->prepare("SELECT field_value
FROM xf_user_field_value
WHERE field_id = 'twitch'
AND field_value != ''"))  {

$result->execute();
$result->bind_result($twitchfield);
$result->store_result();

while($result->fetch())
  {
 
   $data = json_decode(file_get_contents('http://api.justin.tv/api/stream/list.json?channel=' . $twitchfield));
   $viewer[] = $data[]->channel_count;

   if ($data[]->stream != NULL){
       if ($insert = $con->prepare("INSERT INTO twitch_streams (twitchuser, viewercount) VALUES (?, ?)")){

   $insert->bind_param('si', $twitchuser, $viewercount);
   
   $twitchuser = $twitchfield;
   $viewercount = $viewer[];

   $insert->execute();

   echo $twitchfield;
   echo $viewercount;
   $insert->close();
             }
    }
else {
printf("Prepared Insert Statement Error: %s\n", $con->error);
   }

  }

}
else {
printf("Prepared Statement Error: %s\n", $con->error);
}

mysqli_close($con);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.