0
$dungeon = 0;
if($stmt->prepare("SELECT id,name FROM `dungeons` WHERE id > ?")) {
$stmt->bind_param('i',$dungeon);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id,$name);
$stmt2=$stmt;
while($stmt->fetch()) {
    echo "<th>".$name."</th>";
    $query = "SELECT COUNT(id),AVG(dungeon_percent),MAX(dungeon_percent) FROM `users` WHERE dungeon_current = ?"; $what=$id;
    $stmt2->prepare($query);
    $stmt2->bind_param('i',$what);
    $stmt2->execute();
    $stmt2->store_result();
    $stmt2->bind_result($howmany,$avg,$max);
    $stmt2->fetch(); 
    echo "<td align='center'>".$howmany."</td><td align='center'>".round($avg)."%</td><td align='center'>".$max."%</td></tr>";
    }
}

是我所拥有的。然而,当实际上有 4 行时,它只循环一次?谢谢。

4

1 回答 1

1

您是否尝试过创建一个新语句,例如:

 while($stmt->fetch()) {
      $query = "SELECT COUNT(id),AVG(dungeon_percent),MAX(dungeon_percent) 
                FROM `users`
                WHERE dungeon_current = ?"; 
      $what=$id;
      // prepare your new statement using the connection
      $stmt2 = $your_db->prepare($query);
      $stmt2->bind_param('i',$what);
      $stmt2->execute();
      $stmt2->store_result();
      $stmt2->bind_result($howmany,$avg,$max);
      $stmt2->fetch(); 
      echo "<td align='center'>".$howmany.
           "</td><td align='center'>".round($avg).
          "%</td><td align='center'>".$max."%</td></tr>";
  }

我认为一个好主意是在循环之外准备第二个查询。您还应该能够在循环之外调用bind_param()bind_result()然后简单地执行它多次。

这是实现相同目标的更有效方法:

 $query = "SELECT COUNT(id), AVG(dungeon_percent), MAX(dungeon_percent) 
           FROM `users`
           WHERE dungeon_current = ?"; 

 $stmt2 = $your_db->prepare($query);
 $stmt2->bind_param('i', $id);
 $stmt2->bind_result($howmany, $avg, $max);

 while($stmt->fetch()) {
      $stmt2->execute();
      $stmt2->store_result();
      $stmt2->fetch(); 
      echo "<td align='center'>".$howmany.
           "</td><td align='center'>".round($avg).
          "%</td><td align='center'>".$max."%</td></tr>";
  }    
于 2012-04-23T05:55:48.730 回答