Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i count affected rows in SQLite3 with PDO PHP?

Hi i am trying to check if the IP is in the blocked list or not.
I am using SQLite3 in PHP. my problem is when trying to check with the function bellow it returns always true.

    function isBlocked($ip){
        global $pdo;
        $check = $pdo->prepare("SELECT * FROM blockedUsers WHERE ip='".$ip."'");
        $check->execute();
        if($check->rowCount() >= 1){
            return true;
        }else{
            return false;
        }
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use SELECT COUNT(*) rather than SELECT *, since you don’t care about the row data, just the count.

Also, use a parameter rather than substituting the variable into the SQL.

function isBlocked($ip){
    global $pdo;
    $check = $pdo->prepare("SELECT COUNT(*) AS count FROM blockedUsers WHERE ip=:ip");
    $check->execute([':ip' => $ip]);
    $row = $check->fetch(PDO::FETCH_ASSOC);
    return $row['count'] > 0;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading