gpt4 book ai didi

php - 计算左尝试行

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:18 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com