1

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;

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

So what i have is correct? it should be working. And yeah if the pass is empty its still saying there is a pass. is rowcount correct for this?
no. rowCount() is not dependable for SELECT statements. Try if(!empty($query->fetchColumn(0))) instead of if($query->rowCount() > 0)
thanks but im getting a error message, Can't use method return value in write context
you need to do $result = $query->execute();. Then you can try $result->rowCount() or !empty($result->fetchColumn(0))
even for rowCount()??
|
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";
    }
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.