从数据库中获取行,但要在我想要的地方打印每个人



我有很多我想从数据库中检索的行,但我想在我想要的确切位置甚至排行中打印每行。这可能吗?如果是,我该如何实现?

<?php 
$sql="Select * from plays where usertype=2 and idgame=$idgame";
$result=mysqli_query($link, $sql);
while($row =  mysqli_fetch_assoc($result)) {
$aword = $row['word'];
} ?>
<body>
 <div id="row">
      <span class="attemp">1 :</span>
      <span class="word"><?php print  //I want to print here only the 2nd fetched row  ?></span>
  </div>
 <div id="row">
      <span class="attemp">2 :</span>
      <span class="word"><?php print  //I want to print here only the 4th fetched row  ?></span>
  </div>  
</body>

如果在检索数据时,则将其存储到数组中,然后可以按需要输出数据(记住数组为0基于0)...

$aword = [];
while($row =  mysqli_fetch_assoc($result)) {
    $aword[] = $row['word'];
} ?>
<body>
 <div id="row">
      <span class="attemp">1 :</span>
      <span class="word"><?php echo $aword[1];  ?></span>
  </div>
 <div id="row">
      <span class="attemp">2 :</span>
      <span class="word"><?php echo $aword[3];  ?></span>
  </div>  
</body>

另外,我建议,如果您只需要从数据中的一个列中,然后仅选择该列而不是* ...

$sql="Select word from plays where usertype=2 and idgame=".(int)$idgame;

和关于使用准备好的语句停止SQL注入和一些常见错误的强制性评论。

$query = "Select * from plays where usertype=2 and idgame=$idgame";
$result = mysqli_query(isConnect(),$query);
while($row = mysqli_fetch_array($result));
     {
       echo "$row[0]";
      }
     ?>

最新更新