So I have a file that is ran every 5 minutes via the Xenforo Cron interface. It runs fine, and everything seems to be working. But my error logs are filling up with a few errors.
Any ideas what im doing wrong here?
- Undefined offset: 0
- Trying to get property of non-object
- Both say testsql.php:28 (my script)
Code:
<?php
$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())
{
$count = 0;
$data = json_decode(file_get_contents('http://api.justin.tv/api/stream/list.json?channel=' . $twitchfield));
$viewer[0] = $data[0]->channel_count;
if ($insert = $con->prepare("INSERT INTO twitch_streams (twitchuser, viewercount) VALUES (?, ?)")){
$insert->bind_param('si', $twitchuser, $viewercount);
$twitchuser = $twitchfield;
$viewercount = $viewer[0];
$insert->execute();
$insert->close();
}
else {
printf("Insert did not work: %s\n", $con->error);
}
}
}
else {
printf("Prepared Statement Error: %s\n", $con->error);
}
mysqli_close($con);
?>
Any ideas what im doing wrong here?