我有这个 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/