我正在尝试检查数据库中是否设置了密码。但目前它只是说有密码设置
这是我的代码,应该返回“Pass is not in the database”但是它返回“Pass is in the database”
public function checkpass($currentalbumid)
{
$query = $this->db->prepare("SELECT `pass` FROM `album` WHERE `album_id` = ?");
$query->bindValue(1, $currentalbumid);
$query->execute();
if($query->rowCount() > 0){
// password is in the batabase
return "Pass is in the database";
} else {
// password is not in the database
return "Pass is not in the database";
}
}
和这个
$currentalbumid = $_SESSION['album_id'];
$check = $upload->checkpass($currentalbumid);
echo $check;
答案 0 :(得分:1)
有两种可能性:
行可能存在,但返回的pass
值为空。只要数据库中有一个为album_id = 1的条目,它就应该返回一个rowCount为1,无论密码是否在数据库中。
另一种可能是您的数据库配置不允许PDO在SELECT语句上返回rowCount()。 rowCount()是为UPDATE,INSERT和DELETE设计的,所以它对SELECT并不总是很友好。有关详细信息,请参阅此link。
答案 1 :(得分:1)
public function checkpass($currentalbumid)
{
$query = $this->db->prepare("SELECT * FROM `album` where `album_id` = ?");
$query->bindValue(1, $currentalbumid);
$query->execute();
if($query->rowCount() > 0){
// password is in the batabase
while($row = $query->fetch(PDO::FETCH_ASSOC)) {
if($row['pass']){
echo '<input readonly type="password" class="input2" value="locked" /><input type="submit" class="addbtn" value="Locked" />';
} else {
echo '<input type="password" class="input2" id="album_password" placeholder="Want to add a password?" /><input type="submit" class="addbtn" id="lock" value="Lock" />';
}
}
} else {
// password is not in the database
echo "album not found";
}
}
答案 2 :(得分:0)
尝试
$query = $this->db->query("SELECT `pass` FROM `album` WHERE `album_id` = ?");
而不是
$query = $this->db->prepare("SELECT `pass` FROM `album` WHERE `album_id` = ?");