PHP 7.2 - Warnung: count (): Der Parameter muss ein Array oder ein Objekt sein, das Countable implementiert


Marwan Khaled

Ich habe gerade meine PHP-Installation von Version 5.6 auf  7.2 aktualisiert  . Ich habe die count()Funktion auf meiner Anmeldeseite wie folgt verwendet:

if (!empty($_POST['username']) && !empty($_POST['password'])):
    $records = $conn->prepare('SELECT id,username,password FROM users WHERE username = :username');
    $records->bindParam(':username', $_POST['username']);
    $records->execute();
    $results = $records->fetch(PDO::FETCH_ASSOC);

    $message = '';
    
    if (count($results) > 0 && password_verify($_POST['password'], $results['password'])) {
        $_SESSION['user_id'] = $results['id'];
        header("Location: /");
    } else {
        $message = 'Sorry, those credentials do not match';
    }
endif;

Nach der Suche fand ich ähnliche Fragen und Antworten, die jedoch alle mit WordPress zu tun hatten, und ich konnte keine Lösung für Pure PHP finden .

Olim Saidov

PDO fetchgibt bei einem Fehler false zurück. Sie müssen also auch diesen Fall überprüfen:

if($results && count($results) > 0 && password_verify($_POST['password'], $results['password']) ){

    $_SESSION['user_id'] = $results['id'];
    header("Location: /");

} else {
    $message = 'Sorry, those credentials do not match';
}

Verwandte Artikel