【问题标题】:print out rows from database in a table从表中的数据库中打印行
【发布时间】:2017-03-17 00:30:09
【问题描述】:

为什么我需要使用 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";
}

【问题讨论】:

  • &lt;?php $row["testName"] ?&gt;什么都不做。你实际上不是echoing 它。如果 sort-tags 已打开,或者 PHP 高于 5.4.0,您可以执行 &lt;?= $row["testName"] ?&gt;,它会响应它。
  • 1.你忘了echo 变量。 2. 将表格的第一部分和最后一部分移出while 循环(到它之前和之后)。
  • 将第二个选项更改为&lt;td&gt;&lt;?= $row["testName"] ?&gt;&lt;/td&gt;
  • 谢谢大家,我会这么做的

标签: php html mysql while-loop


【解决方案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";
        }

【讨论】:

  • 加 1 并检查,不错的快速修复
  • 感谢继续帮助 :)
【解决方案2】:

这样跑

  <?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 } ?>

【讨论】:

    【解决方案3】:

    说明

    void echo ( string $arg1 [, string $... ] )
    

    Outputs all parameters。没有附加换行符。

    echo 实际上不是一个函数(它是一种语言结构),所以你 不需要与它一起使用括号。回声(不像其他一些 语言结构)的行为不像函数,所以它不能 总是在函数的上下文中使用。另外,如果你想 要将多个参数传递给 echo,参数不能是 括在括号内。

    echo 还有一个快捷语法,您可以立即按照 带等号的开始标签。在 PHP 5.4.0 之前,这个简短的语法 仅适用于启用 short_open_tag 配置设置。

    参考:http://php.net/manual/en/function.echo.php

    【讨论】:

      猜你喜欢
      • 2018-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-11
      • 2015-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多