为什么我需要使用echo将MYSQL数据打印到我的表中?
看看我的代码示例。
我想做什么
在表格中选择并显示查询中的所有数据。
问题1
这有效:<td><?php echo "<br>". $row["testName"]. "<br>";?></td>
这不是:<td><?php $row["testName"] ?></td>
我觉得第二种选择应该有效,但不是。
(这不是一件大事只是感觉不对)
问题2
我还希望所有数据都在一个表中,而不是每次都为循环创建一个新表。
$sql = "SELECT testName, userID FROM `results` WHERE `userID` = '$something' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>some text Score</th>
<th>Date Taken</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td><?php $row["testName"] ?></td>
<td><?php $row["userID"] ?></td>
<td><?php echo "<br>". $row["testName"]. "<br>";?></td>
</tr>
</tbody>
</table>
<?php
}
} else {
echo "0 results";
}
答案 0 :(得分:1)
说明
void echo ( string $arg1 [, string $... ] )
Outputs all parameters
。不会附加额外的换行符。
echo实际上并不是一个函数(它是一种语言结构),所以你 不需要使用括号。回声(与其他一些不同 语言结构)不像一个函数,所以它不能 总是在函数的上下文中使用。另外,如果你想要的话 要将多个参数传递给echo,参数一定不能 括在括号内。
echo也有一个快捷语法,你可以在其中立即关注 用等号打开标签。在PHP 5.4.0之前,这个简短的语法 仅适用于启用了short_open_tag配置设置。
答案 1 :(得分:1)
像这样跑步
<?php
while($row = $result->fetch_assoc()) { ?>
<tr>
<th scope="row">1</th>
<td><?php echo $row["testName"] ?></td>
<td><?php echo $row["userID"] ?></td>
<td><?php echo "<br>". $row["testName"]. "<br>";?></td>
</tr>
<?php } ?>
答案 2 :(得分:1)
如果要显示数据库中的值,则必须使用echo。因为 $ row [“testName”] 包含一些值,但它是值,您可以在回显之后看到它。
要在单个表格中显示数据,请尝试代码:
<?php
$sql = "SELECT testName, userID FROM `results` WHERE `userID` = '$something' ";
$result = $conn->query($sql); ?>
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>some text Score</th>
<th>Date Taken</th>
</tr>
</thead>
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<tr>
<th scope="row">1</th>
<td><?php echo $row["testName"] ?></td>
<td><?php echo $row["userID"] ?></td>
<td><?php echo "<br>". $row["testName"]. "<br>";?></td>
</tr>
<?php } ?>
</table>
<?php }else
{
echo "0 results";
}