【问题标题】:How to make php while loop out of this MySQL query?如何使 php while 循环出这个 MySQL 查询?
【发布时间】:2020-06-04 07:32:14
【问题描述】:

我想创建一个循环,以便每次查询“id”时,都会将数据放入单独的块中。想象一下它就像 ebay 上的列表。我知道 foreach 不会这样做,但我不知道如何执行 while 循环。

<?php
include 'sqlconnection.php';
$conn = OpenCon();

$stmt = $conn->prepare("SELECT id from tasks where status='o'");
$stmt->execute();
$res = $stmt->get_result();
$data = $res->fetch_all(MYSQLI_ASSOC);

foreach ($data as $row) 
{
     echo    "<p>" . $row['id'] . "</p>";   
}
CloseCon($conn);
?>

这是我要重复的 HTML 块:


    <div class="listing-header">
        <div class="listing-title">
          <p>       </p>
        </div>
    </div>

【问题讨论】:

    标签: php html mysql while-loop


    【解决方案1】:

    你可能想看看这个:

    $stmt = $conn->prepare('SELECT id from tasks where status=?'); $o = 'o';
    $stmt->bind_param('s', $o); $stmt->execute(); $stmt->bind_result($id); $html = '';
    while($stmt->fetch()){
      $html .= '<div class="listing-header"><div class="listing-title"><p>'.$id.'</p></div></div>';
    }
    $stmt->close(); $conn->close();
    

    在 HTML 中

    <?=$html?>   
    

    【讨论】:

      猜你喜欢
      • 2014-11-29
      • 1970-01-01
      • 2018-12-14
      • 2023-03-30
      • 2013-03-11
      • 2012-06-10
      • 1970-01-01
      • 2014-11-28
      相关资源
      最近更新 更多