向左计数尝试行

时间:2014-03-18 21:50:40

标签: php mysql mysqli

我有这个MYSQL表:

    id      |      action   
 A6bIMWP1rQ     changedusername
 A6bIMWP1rQ     changedusername

现在我如何使这个PHP函数计数,如果更多5次changedusername exsit,它将返回false?

我试过了:

function checkIfOverFive($id,$mysqli) {
    global $func;  //The database connection

   if ($stmt = $mysqli->prepare("SELECT action FROM userchange_attemps WHERE user_id = ?")) { 
       $stmt->bind_param('i', $id); 
      // Execute the prepared query.
      $stmt->execute();
      $stmt->store_result();
      // If there has been more than 5 failed logins
      if($stmt->num_rows > 5) {
         return true;
      }else{
         return false;
      }
  }
}

如何使用php我确定剩下多少尝试到5? 让我们说现在我的表中有2行,而左3,我如何将该值返回给用户?

感谢。

2 个答案:

答案 0 :(得分:2)

使用mysql COUNT

function checkIfOverFive($id,$mysqli) {
    global $func;  //The database connection

    if ($stmt = $mysqli->prepare("SELECT COUNT(id) as count FROM userchange_attemps WHERE user_id = ? AND action = 'changedusername'")) { 
        $stmt->bind_param('i', $id); 
        // Execute the prepared query.
        $stmt->execute();
        $stmt->store_result();
        $row = $stmt->fetch_assoc();
        if($row['count'] > 5) {
            //do something
        } else {
            //do something else
        }
    }
}

答案 1 :(得分:1)

怎么样......

SELECT COUNT(action)
FROM userchange_attemps
WHERE action = 'changedusername' AND user_id= ?