Php 将数据库中的多行打印到表中
有谁能告诉我如何调整或更改此代码,以便使用PHP将数据库表中的多行打印到html表中?每个用户在表中都有不同数量的记录,因此我需要使用某种循环来打印行Php 将数据库中的多行打印到表中,php,mysql,Php,Mysql,有谁能告诉我如何调整或更改此代码,以便使用PHP将数据库表中的多行打印到html表中?每个用户在表中都有不同数量的记录,因此我需要使用某种循环来打印行 <?php include_once 'dbconfig.php'; $statement = $db_con->prepare("SELECT * FROM entry WHERE user_id=:uid");
<?php
include_once 'dbconfig.php';
$statement = $db_con->prepare("SELECT * FROM entry WHERE user_id=:uid");
$statement->execute(array(":uid"=>$_SESSION['user_session']));
$result=$statement->fetch(PDO::FETCH_ASSOC);
?>
<table>
<tr>
<th>Entry</th>
<th>Date</th>
<th>Weight</th>
<th>BMI</th>
<th>Calories Consumed</th>
<th>Calories Burned</th>
<th>Calorific Deficit</th>
</tr>
<?php
while($row = mysql_fetch_array($result)) {
$entry = $row["entry_id"];
$date = $row["date"];
$weight = $row["weight"];
$bmi = $row["bmi"];
$consumed = $row["calories_consumed"];
$burned = $row["calories_burned"];
$deficit = $row["calorie_deficit"];
echo "<tr>
<td>$entry</td>
<td>$date</td>
<td>$weight</td>
<td>$bmi</td>
<td>$consumed</td>
<td>$burned</td>
<td>$deficit</td>
</tr>";
}
?>
</table>
进入
日期
重量
体重指数
消耗的卡路里
燃烧的卡路里
热量缺乏
这目前工作正常,但它只打印出与特定用户相关的第一行,而不是循环并打印每条记录的一行
更新为包括while循环while($row=$statement->fetch(PDO::fetch_ASSOC)){
$title1=$row[“title1”];
$title2=$row[“titl2”];
$title3=$row[“title3”];
回声“
$title1
$title2
$title3
";
}
虽然(有些事情是真的){do this}
我已经用我现在的答案更新了我的答案,但是由于某种原因,它没有在标题行之后打印任何行。你将PDO与mysql
混合,你不能这样做。我之前尝试过类似的方法,现在尝试了这种方法。出于某种原因,它没有将任何数据返回到表中。我已经尝试了这段代码,它似乎是一种改进,因为在使用inspector工具进行检查时添加了相关行。但是数据仍然没有被添加到每个单元格中!我将while循环的参数更改为while($row=$statement->fetch(PDO::fetch_ASSOC)),这解决了我的问题。谢谢你的帮助,德拉维亚!
while ($row =$statement->fetch(PDO::FETCH_ASSOC)) {
$title1 = $row["title1"];
$title2 = $row["titl2"];
$title3 = $row["title3"];
echo "<tr>
<td>$title1</td>
<td>$title2</td>
<td>$title3</td>
</tr>";
}