php - 计算左尝试行

标签 php mysql mysqli

我有这个 MYSQL 表:

    id      |      action   
 A6bIMWP1rQ     changedusername
 A6bIMWP1rQ     changedusername

现在我如何让这个 php 函数计算如果超过 5 次更改的用户名存在,它会返回 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 行,我如何将该值返回给用户?

谢谢。

最佳答案

使用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
        }
    }
}

关于php - 计算左尝试行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22491801/

相关文章:

php - 我的 sql 文件上传需要 "if/else"语句

php - cakephp:如何在default.ctp中添加下拉列表?

php - mysqli 动态生成的表,带有打开包含行中所有值的表单的链接

php - SimpleXML IF 条件

php - 如何在一列mysql中放置多个关系ID?

php mysql 表中没有的唯一随机数 - 检查然后生成

PHP准备好的语句不起作用

php - 将文件从一个 ftp 传输到另一个

mysql - Mac osx 上的 Apache 超集

PHP 仅显示来自 4 的第一张图片