$stmt->fetchAll(PDO::FETCH_ASSOC);没有结果

$stmt->fetchAll(PDO::FETCH_ASSOC); No results

当我尝试使用 $stmt->fetch(PDO::FETCH_ASSOC) 时,没有给出结果;它给了我 3 倍相同的结果。

    $sql = "SELECT char_name, pvpkills FROM characters WHERE accesslevel=:normalPlayer AND pvpkills>=:minPvp ORDER BY pvpkills DESC LIMIT :maxRows";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':normalPlayer', $normalPlayer, PDO::PARAM_INT);
            $stmt->bindParam(':minPvp', $minPvp, PDO::PARAM_INT);
            $stmt->bindParam(':maxRows', $maxRows, PDO::PARAM_INT);
            $stmt->execute();
            $cuentaPvp = $stmt->rowCount();
            $resPvp = $stmt->fetchAll(PDO::FETCH_ASSOC);

            Session::init();
            Session::set('cuenta_pvp', $cuentaPvp);

            for($i = 0; $i<$cuentaPvp; $i++){
                Session::set('pvp_name'.$i, $resPvp['char_name']);
                Session::set('pvp_count'.$i, $resPvp['pvpkills']);
            }

你为什么不在 $stmt->fetchAll(...) 中使用 while 循环?

fetchAll 的结果是一个结构数组,因此您可以将 for 语句替换为:

foreach ($resPvp as $key => $value) {
    Session::set('pvp_name' . $key, $value['char_name']);
    Session::set('pvp_count' . $key, $value['pvpkills']);
}

将 for 循环更改为以下代码:

for($i = 0; $i<$cuentaPvp; $i++){
                Session::set('pvp_name'.$i, $resPvp[$i]['char_name']);
                Session::set('pvp_count'.$i, $resPvp[$i]['pvpkills']);
            }