【问题标题】:How to put a foreach in a while loop如何在while循环中放置一个foreach
【发布时间】:2019-09-13 09:48:47
【问题描述】:

我不知道如何在我的 while 循环中使用 foreach。

我已经尝试理解这些帖子,但我无法弄清楚:

Stack Post

Stack Post

我也尝试在其他网站(如 w3)上找到它。

这是我的代码:

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM data";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table class='table'>
    <tr>
    <th>ID</th>
    <th>date</th>
    <th>security</th>
    <th>photo</th>
    </tr>";
    // output data of each row
 while ($row = $result->fetch_assoc()) {
     echo "<tr>
        <td>" . $row["id"]. "</td>
        <td>" . $row["Date"]."</td>
        <td>" . $row["Security"]. "</td>
        <td>" . $row["Photo"]. "</td>
        </tr></table>";
    }
} else {
    echo "No data found";
}

$conn->close();
?>

我从数据库表中只得到一行,其他的输出如下:

2019-09-12 15:24:23 0 6 2019-09-12 15:30:09 1 7 2019-09-12 15:30:33 1 8 2019-09-12 15:30:33 1 9 2019-09-12 15:30:33 1 10 2019-09-12 15:32:39 1 11 2019-09-12 15:32:39 1 12 2019-09-12 15:32:39 1 13 2019 -09-12 15:32:39 1 14 2019-09-12 15:32:39 1 15 2019-09-12 15:32:39 1 16 2019-09-12 19:26:36 1 17 2019-09 -12 19:26:42 1 18 2019-09-12 19:35:15 1 19 2019-09-12 19:35:15 1 20 2019-09-12 19:35:15 1 21 2019-09-12 19:35:15 1 22 2019-09-12 19:35:15 1 23 2019-09-12 19:35:15 1 24 2019-09-12 19:35:15 1 25 2019-09-12 19: 35:15 1 26 2019-09-12 19:35:15 1 27

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    您可以将 foreach 与 fetch-all 方法一起使用

    【讨论】:

      【解决方案2】:
      <?php
      // Create connection
      $conn = new mysqli($servername, $username, $password, $dbname);
      // Check connection
      if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
      } 
      $sql = "SELECT * FROM data";
      $result = $conn->query($sql);
      
      if ($result->num_rows > 0) {
          ?> 
         <table class='table'>
          <tr>
          <th>ID</th>
          <th>date</th>
          <th>security</th>
          <th>photo</th>
          </tr>
          <?php
          // output data of each row
           while ($row = $result->fetch_assoc()) 
           {
               ?>
             <tr>
              <td><?=  $row["id"];?></td>
              <td><?=  $row["Date"];?></td>
              <td><?=  $row["Security"];?></td>
              <td><?=  $row["Photo"];?></td>
              </tr>
              <?php
          }
          ?>
          </table>
          <?php
      } else {
          echo "No data found";
      }
      
      $conn->close();
      ?>
      

      【讨论】:

        【解决方案3】:

        您需要更改如下代码:-

        <?php
            // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);
            // Check connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            }
            $sql = "SELECT * FROM data";
            $result = $conn->query($sql) or die($conn->error);//get query error if occur
            $rows = $result->fetch_all(MYSQLI_ASSOC); //use fetch_all()
        ?>
        <table class='table'>
            <tr>
                <th>ID</th>
                <th>date</th>
                <th>security</th>
                <th>photo</th>
            </tr>
            <?php
                if ($result->num_rows > 0) {
                    foreach($rows as $row) { //apply foreach()
            ?>
                        <tr>
                        <td><?php echo $row["id"] ;?></td>
                        <td><?php echo $row["Date"] ;?></td>
                        <td><?php echo $row["Security"] ;?></td>
                        <td><?php echo $row["Photo"] ;?></td>
                        </tr>
            <?php
                    }
                } else {
            ?>
                <tr colspan="4">No data found</tr>
            <?php
                }
                $conn->close();
            ?>
        </table>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-02-04
          • 2014-10-19
          • 2012-10-14
          • 2015-05-10
          • 1970-01-01
          • 2013-10-01
          • 2013-07-06
          • 2011-08-06
          相关资源
          最近更新 更多