【问题标题】:PHP echoing MySQL data into HTML tablePHP 将 MySQL 数据回显到 HTML 表中
【发布时间】:2015-10-21 16:10:08
【问题描述】:

所以我正在尝试制作一个 HTML 表,该表从 MySQL 数据库中获取数据并将其输出给用户。我正在使用 PHP,我对它非常陌生,所以请原谅我的 messy code

我正在使用的代码是:大括号“你的代码太糟糕了!”

<table class="table table-striped table-hover ">
    <thead>
    <tr>
        <th>#</th>
        <th>Name</th>
        <th>Description</th>
        <th>Reward</th>
        <th>Column heading</th>
    </tr>
    </thead>
    <tbody>
    <?php
        $con = mysql_connect("localhost", "notarealuser", 'notmypassword');
        for ($i = 1; $i <= 20; $i++) {
            $items = ($mysqli->query("SELECT id FROM `items` WHERE id = $i"));
            echo ("<tr>");
            echo ("
                <td>
                while ($db_field = mysqli_fetch_assoc($items)) {
                    print $db_field['id'];
                }</td>");
            $items = ($mysqli->query("SELECT name FROM `items` WHERE id = $i"));
            echo ("
                <td>
                while ($db_field = mysqli_fetch_assoc($items)) {
                    print $db_field['name'];
                }</td>");
            $items = ($mysqli->query("SELECT descrip FROM `items` WHERE id = $i"));
            echo ("
                <td>
                while ($db_field = mysqli_fetch_assoc($items)) {
                    print $db_field['descrip'];
                }</td>");
            $items = ($mysqli->query("SELECT reward FROM `items` WHERE id = $i"));
            echo ("
                <td>
                while ($db_field = mysqli_fetch_assoc($items)) {
                    print $db_field['reward'];
                }</td>");
            $items = ($mysqli->query("SELECT img FROM `items` WHERE id = $i"));
            echo ("
                <td>
                while ($db_field = mysqli_fetch_assoc($items)) {
                    print $db_field['img'];
                }</td>");
            echo ("</tr>");
        }
    ?>
    </tbody>
</table>

但是,此代码不起作用 - 它只会导致页面立即输出 500 Internal Server Error。 IIS 日志将其显示为 500:0 - 通用 ISE。有什么想法吗?

【问题讨论】:

  • 请更具体一些。只问Anyone have any ideas 无助于了解问题所在。
  • 您似乎认为您可以将 PHP 代码回显到浏览器以在那里运行它。 NO NO No PHP 代码只在服务器上运行
  • 你应该读一点。你应该有一个数据库类。我建议你尝试一个轻量级的框架。你会看到一切都会锻炼得更顺利......
  • 500 Internal Server Error 表示您在 PHP 中有错误。您需要在服务器上启用错误处理,以便它向您显示错误是什么。否则可能很难调试,尤其是根据您的知识水平。
  • 所以,你没有时间,你没有知识,你决定利用别人的时间。

标签: php html mysql echo html-table


【解决方案1】:

您正在混合使用 mysql 和 mysqli,而不是关闭 php 代码块并且您没有选择数据库。另外,您不必为每个字段运行查询

试试这个:

<table class="table table-striped table-hover ">
    <thead>
        <tr>
            <th>#</th>
            <th>Name</th>
            <th>Description</th>
            <th>Reward</th>
            <th>Column heading</th>
        </tr>
    </thead>

    <tbody>
        <?php
            $con = new mysqli("host","user", "password", "database");

            $execItems = $con->query("SELECT id, name, descrip, reward, img FROM `items` WHERE id BETWEEN 1 AND 20 ");

            while($infoItems = $execItems->fetch_array()){
                echo    "
                            <tr>
                                <td>".$infoItems['id']."</td>
                                <td>".$infoItems['name']."</td>
                                <td>".$infoItems['descrip']."</td>
                                <td>".$infoItems['reward']."</td>
                                <td>".$infoItems['img']."</td>
                            </tr>
                        ";

            }
        ?>
    </tbody>
</table>

【讨论】:

  • 成功了,非常感谢!后来才意识到数据库的事情,但没有看到我没有关闭PHP标签。
【解决方案2】:
<table class="table table-striped table-hover">
      <thead>
          <tr>
              <th>#</th>
              <th>Name</th>
              <th>Description</th>
              <th>Reward</th>
              <th>Column heading</th>
          </tr>
      </thead>
      <tbody>
      <?php
      $con = mysqli_connect("hostname","username",'password');
      $sql= "SELECT * FROM `items` WHERE id <20 ";
      $items = (mysqli_query($sql));
      while ( $db_field = mysqli_fetch_assoc($items) ) {?>
          <tr><td><?php echo $db_field['id'];?></td></tr>
          <tr><td><?php echo $db_field['name'];?></td></tr>
          <tr><td><?php echo $db_field['descrip'];?></td></tr>
          <tr><td><?php echo $db_field['reward'];?></td></tr>
          <tr><td><?php echo $db_field['img'];?></td></tr>
      <?php}
      </tbody>
</table>

尝试这些,未测试

【讨论】:

  • 尝试使用mysqli或PDO(目前代码同时使用,显然不行)
  • 您可以听取自己的建议。您正在使用 mysql_mysqli_ 也不起作用
【解决方案3】:

问题在哪里?

这段代码有很多问题。 首先,您对 PHP 和 HTML 感到困惑。 之间的代码是PHP。它在服务器上执行,你可以在那里有循环、变量和赋值。如果你想要一些 HTML,你可以使用“echo”。

外部代码是 HTML - 它按原样发送到浏览器。

第二 - 您似乎正在做的是分别查询每个字段。这不是您使用 SQL 的方式。

以下是您需要做的或多或少的事情:

//Query all rows from 1 to 20:
$items = $mysqli->query("SELECT id,name,descrip,reward,img FROM `items` WHERE id between 1 and 20");    
//Go through rows    
while ( $row = mysqli_fetch_assoc($items) ) 
{
   echo "<tr><td>{$db_field['id']}</td>";
   //echo the rest of the fields the same way 

});

【讨论】:

    【解决方案4】:

    我将继续假设代码不起作用,这是因为存在几个基本错误。我强烈建议您仔细阅读 PHP 的主题,特别是因为您使用的是数据库,如果使用不安全的代码访问这些数据库,可能会带来重大的安全风险。

    首先,您已经使用过程 mysql_connect 函数设置了连接,但接下来的几行您通过尝试在非对象上调用方法 mysqli::query 切换到面向对象,因为它是在连接期间从未实例化。

    http://php.net/manual/en/mysqli.construct.php

    其次,PHP echo() 不需要括号。 PHP 有时将其描述为一个函数,但它是一种语言结构,如果您尝试解析多个参数,括号会导致问题。

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

    第三,您不能简单地从 HTML 和 PHP 切换,反之亦然,通知服务器/浏览器。如果您希望这样做,您需要连接...

    echo "<td>".while($db_filed = mysqli_fetch_assoc($item)) {
         print $db_field['id'];
    }."</td>;
    

    或者最好(在我看来它看起来更干净)

    <td>
        <?php 
        while($db_filed = mysqli_fetch_assoc($item)) {
            print $db_field['id'];
        }
        ?>
    </td>
    

    但是,这些示例基于您的代码,该代码将每个 ID 输出到同一个单元格中,我认为这不是您的目标,因此您也应该将单元格插入循环中,以便每个 ID 都属于自己的细胞。此外,我建议使用 echo over print(它更快)。

    现在可能不是问题但可能演变成问题的其他事情是您为 FOR 循环使用了一个常量。如果您需要从表中提取超过 20 行,那么您将不得不手动增加此数字,如果您的表少于 20 行,您将收到错误,因为循环将尝试访问不存在的表行'不存在。

    我不是 PHP 专家,所以我的一些术语可能不正确,但希望我所掌握的知识会有所帮助。同样,我强烈建议您在使用该语言之前先熟悉该语言。

    【讨论】:

      猜你喜欢
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      • 1970-01-01
      相关资源
      最近更新 更多