Limit the number of requests in the query

0

I have query that does one request at a time, in random order.

$sql = "SELECT * FROM 'viperusers' WHERE vip < 1 AND username != :username ORDER BY RAND() LIMIT 1";

Explaining, I select all my users, if it is mine ignore, as you can see I limited to 1 for each reload on the page to get a random user.

Every time I pass a user, I count +1 of my Database, that is, every request adds +1, but I have a problem, I wish that when I selected the same user, I would not count.

  • Can you do this?

Here's the code:

public function counter($select_data, $following, $is_vip) {
  global $followersLimit, $follow;

  $followersAttempts = (int) $select_data[0]->count;

  if (($is_vip && $follow->_return->status == 1) 
        || !$is_vip) {
      $followersAttempts++;
  }

  if ($followersAttempts > $select_data[0]->count) {
    $this->_db->update('viperusers', array(
      'count' => $followersAttempts
    ), array(
      'username' => $select_data[0]->username
    ));
  }

  if ($followersAttempts >= $followersLimit) {
      $timer = ($is_vip) 
        ?$this->select_configs()[0]->timer_vip 
        :$this->select_configs()[0]->timer_free;

      $select_data[0]->timer = strtotime('+' . $timer . ' minutes');

      $this->_db->update('viperusers', array(
        'count' => 0,
        'timer' => $select_data[0]->timer
      ), array(
        'username' => $select_data[0]->username
      ));

      return json_encode(array(
        'follow'  => false,
        'timer'   => $timer
      ));
  }

  return json_encode(array(
    'follow'    => true,
    'followed'  => $select_data[0]->username,
    'following' => $following,
    'count'     => $followersAttempts
  ));
}

public function followers() {
  global $followersLimit, $follow;

  if (isset($_SERVER['REQUEST_METHOD']) 
      && $_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = Session::get('username');

    $select_data = $this->_requests->select_data($username);

    if ($select_data[0]->timer > time()) {
      echo json_encode(array(
        'follow'  => false,
        'timer'   => ceil((($select_data[0]->timer - time()) / 60))
      ));

      exit;
    }

    $select_random = $this->_requests->select_random($username);

    $follow = new Follow;

    $follow->setCookieFile(SMVC . 'application' . 
                              DS . 'cookies' . DS . $select_random[0]->username . '.txt');

    $follow->initialize( $select_data[0]->ds_user_id, 
        $select_random[0]->csrftoken );

    if ($follow->_return->status != $select_random[0]->status) {
      $this->_requests->set_status($select_random[0]->username, $follow->_return->status);
    }

    $followersLimit = $this->_requests->select_configs()[0]->followers_x;

    if ($this->_requests->select_is_vip($username)) {
      $is_vip = true;
      $followersLimit = 2 * $followersLimit;
    } else {
      $is_vip = false;
    }
    echo $this->_requests->counter($select_data, 
                                    $select_random[0]->username, $is_vip);
  }
}

Here's verifying vips:

public function select_random($username) {
    if ($this->select_is_vip($username)) {
        $sql = "SELECT * FROM 'viperusers' WHERE vip < 1 AND username != :username ORDER BY RAND() LIMIT 1";
    } else {
        $sql = "SELECT * FROM 'viperusers' WHERE vip = 0 AND username != :username ORDER BY RAND() LIMIT 1";
    }

    $query = $this->_db->select($sql, array(
        ':username' => $username
    ));

    if (empty($query[0]->username)) {
        return $this->select_random($username);
    }

    return $query;
}
    
asked by anonymous 02.12.2017 / 03:07

1 answer

0

Some details I noticed: 1) I have not seen what the select_is_vip function does, but because you no longer save it if the user is vip here: 'username' = > $ select_data [0] -> username, 'is_vip' = > ($ select_data [0] -> vip == 0? false: true) This avoids calling the select_is_vip function. 2) Where are you storing in the database this plus 1?

But if you do this, there will come a time that you will no longer have users. For example, if in your database you have 10 users and give 10 refresh's you will have no user. I say this because in your function you are running the risk of entering an infinite loop when this happens, because it has this command line: if (empty ($ query [0] -> username)) {         return $ this- > select_random ($ username);     }

    
02.12.2017 / 17:08