我将从上一页获得team_name,然后在浏览器中显示具有该团队名称的所有球员。
但这会产生错误,当我尝试打印变量$players时,只有该队名的一名球员的详细信息。
我认为查询结果必须存储在某种二维数组中,但我无法考虑其背后的逻辑。

玩家表具有属性


选手姓名
jearsy_no
队名
电话号码
多宝


<?php
 include('config/db_connect.php');
 if(isset($_GET['team_name'])) {
  $team_name = mysqli_real_escape_string($conn, $_GET['team_name']);
  $sql_players = "SELECT * FROM player WHERE team_name = '$team_name'";
  $result_players = mysqli_query($conn, $sql_players);
  $players = mysqli_fetch_assoc($result_players);
  //print_r($players);
 }
?>
<!DOCTYPE html>
<html>
<?php include('templates/header.php'); ?>

  <?php foreach($players as $player) : ?>
  <div class="container">
        <h6><?php echo htmlspecialchars($player['player_name']); ?></h6>
  </div>
  <?php endforeach; ?>

<?php include('templates/footer.php'); ?>
</html>


我收到此错误:


  警告:字符串偏移量'player_name'不合法


当我尝试print_r($players)时,我得到:

  Array ( [player_name] => Dwayn Miller
          [jearsy_no] => 32
          [team_name] => Team Australia
          [dob] => 1998-12-11
          [player_phone] => 4009871234
        )

最佳答案

我有一个解决方案,而不是

$players = mysqli_fetch_assoc($result_players);


如果我使用mysqli_fetch_all(),则可以获取该团队名称的所有球员详细信息

$players = mysqli_fetch_all($result_players, MYSQLI_ASSOC);

10-08 01:14