使用 php 和 mysqli 显示表数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23203636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:34:02  来源:igfitidea点击:

Displaying table data with php & mysqli

phpmysqliismysqlihtml-table

提问by chromatel

I am trying to display the data from my table on my page and I have done it many times before. This time it is not working, I usually copy the code from my other projects and change the appropriate values but this time it's not working. Please note I am not quite sure what I am doing. I don't usually write code. This is using iis with php and the database server is mysql. The problem is I get a white page with no errors or other signs.

我试图在我的页面上显示我的表格中的数据,我以前已经做过很多次了。这次它不起作用,我通常从我的其他项目中复制代码并更改适当的值,但这一次它不起作用。请注意,我不太确定我在做什么。我通常不写代码。这是使用 iis 和 php,数据库服务器是 mysql。问题是我得到一个没有错误或其他迹象的白页。

Here is the code.

这是代码。

<?php
require('connection.php');

$sql = "SELECT * FROM td";

$result = mysqli_query($con,$sql)or die(mysqli_error());

echo "<table>";
echo "<tr><th>Date</th><th>Comment</th><th>Amount</th></tr>";

while($row = mysqli_fetch_array($result)) {
    $date = $row['date'];
    $comment = $row['comment'];
    $amount = $row['amount'];
    echo "<tr><td style='width: 200px;'>".$date."</td><td style='width: 600px;'>".$comment."</td><td>".$amount."</td></tr>";
} 

echo "</table>"
mysqli_close($con);
?>

Oh and there is data in the table. Also the connection to the db is fine I use the same connection file to insert data to the table.

哦,表中有数据。与数据库的连接也很好,我使用相同的连接文件将数据插入到表中。

Wow that fixed it. I knew it was some stupidity on my part I just did notice it.

哇解决了它。我知道这是我的一些愚蠢,我只是注意到了。

回答by Pain67

echo "</table>";

you forget the ";" :D. And if that did not help, maybe the result is empty and there's nothing to show? That may be why you got a white screen.

你忘记了“;” :D。如果这没有帮助,也许结果是空的,没有什么可显示的?这可能就是你看到白屏的原因。