2 回答
TA贡献1818条经验 获得超7个赞
rowCount当用户名或电子邮件匹配时,方法返回 >= 1 条记录。试试那个代码:
$username = $_POST['username'];
$email = $_POST['email'];
$errors = array();
$sql = "SELECT username, email FROM users WHERE username = :username OR email = :email";
$stmt1 = $pdo->prepare($sql);
$stmt1->bindParam(":username", $username, PDO::PARAM_STR);
$stmt1->bindParam(":email", $email, PDO::PARAM_STR);
$stmt1->execute();
if($stmt1->rowCount() > 0){
$rows = $stmt1->fetchAll();
foreach($rows as $row) {
if($row['username'] === $username) {
$errors['username'] = "Username already in use.";
}
if($row['email'] === $email) {
$errors['email'] = "Email already in use.";
}
}
$stmt1->closeCursor();
}
TA贡献1946条经验 获得超4个赞
你只是从这个查询中得到计数
$sql = "SELECT uid FROM users WHERE username = :username OR email = :email";
代替
$sql = "SELECT uid,count(uid) FROM users WHERE username = :username OR email = :email";
希望它对单个查询有所帮助
- 2 回答
- 0 关注
- 70 浏览
添加回答
举报