简体   繁体   中英

php rowcount is not working

I am trying to check if there is a password set in the database. But at the moment its just saying that there is a password set

here is my code, is should return with "Pass is not in the database" but its returning with "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";
    }
}

and this

    $currentalbumid = $_SESSION['album_id'];

    $check = $upload->checkpass($currentalbumid);

    echo $check;

There are 2 possibilities:

It is possible that the row exists, but the pass value that is returned is empty. As long as there is an entry in the database for album_id = 1, it should return a rowCount of 1, regardless of if the password is in the database.

The other possibility is that your database configuration does not permit PDO to return rowCount() on SELECT statements. rowCount() is designed for UPDATE, INSERT, and DELETE so it isn't always friendly with SELECT. See this link for more information.

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";
    }
}

Try

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

Instead of

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM