php - Using php and the while loop to print a table of data that is being selected from a mysql db
问题描述
I am new at php and am trying to learn CRUD applications. For this part of the code I have a table set up in a database (mysql). I am trying to interact with the table. I want a user to login (that part works) and when they are logged in- I store a message in $_SESSION that says ie. 'success'. This lets me know the user is logged in. Not the problem. The second piece is that IF there are rows IN THE TABLE already (from previous sessions) I want to print out these rows in a table. If there are no rows I want to print out "No rows"
I am trying to use simple logic here where first I create the $row variable by doing a fetch that comes from a pdo object(also fine). If that row is TRUE ie. it exists, print the table. If that row is false, print no rows. However, I keep getting the whole table printed, say 5 times. Instead of the 5 rows in the table.
Also, I am not sure I am using the correct type of while () statement. Which right now reads,
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
And then I get the WHOLE table and all rows 5 whole times. fetch and PDO::FETCH_ASSOC are a bit unclear to me, and I am understanding the $row as either true or false.
$stmt= $pdo->query("SELECT make, model, year, mileage from autos");
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ( isset($_SESSION['success']) && $row == true ) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo ('<table border="1">'."\n");
echo "<tr><td>";
echo "Make";
echo ("</td><td>");
echo "Model";
echo ("</td><td>");
echo "Year";
echo ("</td><td>");
echo "Mileage";
echo ("</td><td>");
echo "Action";
echo ("</td></tr>");
echo "<tr><td>";
echo (htmlentities($row['make']));
echo ("</td><td>");
echo (htmlentities($row['model']));
echo ("</td><td>");
echo (htmlentities($row['year']));
echo ("</td><td>");
echo (htmlentities($row['mileage']));
echo ("</td><td>");
echo ('<a href="edit.php?user_id='.$row['user_id'].'">Edit</a>'.'/'.'<a
href="delete".php?user_id='.$row['user_id'].'>Delete</a>');
echo "</td></tr>";
}
} elseif ( isset($_SESSION['success']) && $row == false ){
echo "No rows found";
}
解决方案
I keep getting the whole table printed, say 5 times.
Because you're printing the whole table on each iteration of the loop:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo ('<table border="1">'."\n");
// etc.
Instead of checking whether the first row is true, check the row count:
$stmt= $pdo->query("SELECT make, model, year, mileage from autos");
$count = $stmt->rowCount();
if ( isset($_SESSION['success']) && $count > 0 )
Then output your table outside the loop and just output each row inside the loop:
echo ('<table border="1">'."\n");
// etc, output the table header
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<tr><td>";
// etc.
echo "</td></tr>";
}
echo ('</table>'."\n");
推荐阅读
- python - Python线性回归组合问题
- php - WooCommerce 的 Mailchimp:删除同步问题的订阅者标签
- amazon-s3 - AWS Glue 和 S3 访问点
- node.js - 如何在 CodeIgniter 4 中集成节点包?
- bash - Bash脚本完成后没有退出,为什么?
- angularjs - 健全性检查:是否可以在 HLS(m3u8)视频流和 angularjs 应用程序(ui)之间进行代理?
- python - 用列表理解解决方案替换 numpy isin
- flutter - Flutter 设置状态未使用新数据更新我的 UI
- python - 瑞典语字符解码错误:这是 API 的错还是我的错?
- python - 无法创建 urls.yaml 以使用 urlwatch