1

我正在尝试检查数据库中是否设置了密码。但目前它只是说设置了密码

这是我的代码,应该返回“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;
4

3 回答 3

1

有2种可能:

该行可能存在,但pass返回的值为空。只要数据库中存在album_id = 1 的条目,无论密码是否在数据库中,它都应返回rowCount 1。

另一种可能性是您的数据库配置不允许 PDO 在 SELECT 语句上返回 rowCount()。rowCount() 是为 UPDATE、INSERT 和 DELETE 设计的,因此它对 SELECT 并不总是友好的。有关更多信息,请参阅此链接

于 2013-09-16T19:31:31.000 回答
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";
    }
}
于 2013-09-16T22:04:37.317 回答
0

尝试

$query = $this->db->query("SELECT `pass` FROM `album` WHERE `album_id` = ?"); 

代替

$query = $this->db->prepare("SELECT `pass` FROM `album` WHERE `album_id` = ?"); 
于 2017-09-21T08:18:02.313 回答