【问题标题】:How to put mysqli while result into one variable and echo it in html如何将 mysqli while 结果放入一个变量并在 html 中回显
【发布时间】:2017-10-08 03:17:39
【问题描述】:

如何将mysqli select中的所有while结果放入一个变量并回显到html文档中?

代码基于 W3school PHP

    $sql = "SELECT firstname, lastname FROM MyGuests";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
//previously it was  echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
";
$parsedcontent = '<div class="col-md-4">'.$row["firstname"].'</div><div class="col-md-4">'.$row["lastname"].'</div>';
        }
    } else {
        echo "0 results";
    }

HTML

<div class="row">
    <?php echo $parsedcontent; ?>
</div>

【问题讨论】:

  • 你需要使用.=连接字符串 -> $parsedcontent .= '&lt;div class="col-md-4"&gt;'...
  • 怎么做?

标签: php html mysqli


【解决方案1】:
$data = array();
// output data of each row
 while($row = $result->fetch_array(MYSQLI_ASSOC)) {
    // push retrieved data to data array in order for return later
    array_push($data, $row);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    相关资源
    最近更新 更多