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

Time:03-04

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

CodePudding user response:

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;
}
  • Related