当我尝试使用$ 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']);
}
答案 0 :(得分:1)
fetchAll
的结果是一个结构数组,因此您可以将for
语句替换为:
foreach ($resPvp as $key => $value) {
Session::set('pvp_name' . $key, $value['char_name']);
Session::set('pvp_count' . $key, $value['pvpkills']);
}
答案 1 :(得分:0)
为什么不使用带$stmt->fetchAll(...)
的while循环?
答案 2 :(得分:0)
将for循环更改为以下代码:
for($i = 0; $i<$cuentaPvp; $i++){
Session::set('pvp_name'.$i, $resPvp[$i]['char_name']);
Session::set('pvp_count'.$i, $resPvp[$i]['pvpkills']);
}