Daniel Lematy
Daniel Lematy

Reputation: 167

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;

Upvotes: 1

Views: 1874

Answers (3)

Onsong Norcio
Onsong Norcio

Reputation: 1

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` = ?"); 

Upvotes: 0

Daniel Lematy
Daniel Lematy

Reputation: 167

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

Upvotes: 1

XaxD
XaxD

Reputation: 1538

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.

Upvotes: 1

Related Questions