Simple Cron SQL Job

rivacom

Active member
Hi everyone, I have some code I want to enable for a cron job to update a table based on a custom field. I'm trying to convert my straight PHP script to what I think is Xenforo friendly but I could be wrong. However I seem to be having issues with the insert query, can anyone comment or even let me know if the whole thing has been done wrong? Thanks!

PHP:
<?php

class twitch_cron {
protected static $queries = array(
        'deleteQuery' => 'TRUNCATE TABLE `xf_twitch_streams`',
        'userQuery' => 'SELECT * FROM xf_user_field_value WHERE field_id = "twitch" AND field_value <> ""',
        'insertQuery' => 'INSERT INTO `xf_twitch_streams` (twitch_username, twitch_viewers) VALUES ($data[0],$viewer[0])'
    );
   
    public static function getUpdate()
    {

        $db = XenForo_Application::get('db');
        $db->query(self::$queries['deleteQuery']);
        $db->fetchall(self::$queries['userQuery']);
       
        foreach($db as $user) {
            $twitchuser[] = $user['field_value'];
            $data = json_decode(file_get_contents('http://api.justin.tv/api/stream/list.json?channel=' . $twitchuser[0]));
            $viewer[0] = $data[0]->channel_count;
            $db->query(self::$queries['insertQuery']);
        }
       
    }
}
    ?>
 
Top Bottom