$access_token = $_SESSION['access_token'];
$connectionT = new TwitterOAuth($consumer_key, $consumer_secret, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$servername = "localhost";
$username = "******";
$password = "";
$dbname = "twitterdata";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$currentUser = $_SESSION['cUser'];
$sqlquery = "Select * from registered where userID={$currentUser}";
if ($result = mysqli_query($conn, $sqlquery)) {
    $row_cnt = mysqli_num_rows($result);
    if ($row_cnt == 0) {
        $limit = checkRateLim($connectionT);
        //print_r($limit);
        if ($limit > 0) {
            $row_cnt = mysqli_num_rows($result);
            if ($row_cnt == 0) {
                $user = $connectionT->get("followers/ids", array("count" => '5000'));
                //print_r($user);
                $array = $user->ids;
                $sql = "INSERT INTO registered (userID) VALUES({$currentUser})";
                if ($conn->query($sql) === TRUE) {
                    //echo "NEW RECORD CREATED SUCCESSFULLY";
                }
                //	print_r($array);
                saveConnections($currentUser, $array, $conn, $connectionT);
                deleteFile();
                createFile();
function saveConnections($loggedUser, $followerArray, $database, $twitConn)
{
    $user = $loggedUser;
    $followers = $followerArray;
    $conn = $database;
    $j = sizeof($followers);
    $connectionT = $twitConn;
    for ($i = 0; $i < $j; $i++) {
        $source = $followers[$i];
        $sqlquery = "Select * from userdata where source={$source} AND target={$user}";
        if ($result = mysqli_query($conn, $sqlquery)) {
            $row_cnt = mysqli_num_rows($result);
            if ($row_cnt == 0) {
                $query = "INSERT INTO userdata (source,target) VALUES({$source},{$user})";
                if ($conn->query($query) === TRUE) {
                    //echo "Saved connection";
                }
            }
        }
        $newQuery = "Select * from gather where userID={$source}";
        if ($result = mysqli_query($conn, $newQuery)) {
            $row_cnt = mysqli_num_rows($result);
            if ($row_cnt == 0) {
                $query = "INSERT INTO gather (userID) VALUES({$source})";
                if ($conn->query($query) === TRUE) {
                    //echo "Saved connection";
                }
            }
        }
    }
    $limit = checkRateLim($connectionT);
    $query = mysqli_query($conn, "Select * from gather");
    $array = array();
    while ($row = mysqli_fetch_assoc($query)) {
        // add each row returned into an array
        $array[] = $row;
    }
    $data = $array;
    print_r($data);
    $go = sizeof($data);
    for ($i = 0; $i < $go; $i++) {
        $userTemp = $data[$i];
        $user = $userTemp['userID'];
        //print_r($user);
        try {
            $limit = checkRateLim($connectionT);
            if ($limit > 0) {
                $level2 = $connectionT->get("followers/ids", array("user_id" => $user, "count" => '5000'));
                $array = $level2->ids;
                //print_r($array);
                if (is_array($array)) {
                    $query = "DELETE FROM gather WHERE userID={$user}";
                    if ($conn->query($query) === TRUE) {
                        saveThis($user, $array, $conn);
                        //echo "Saved connection";
                    }
                }
            } else {
                echo "No more data is available at this time";
            }
        } catch (Exception $e) {
            echo "This is all the data for now";
        }
    }
}