twitter-bootstrap 使用 PHP 使用 MySQL 数据填充 HTML Bootstrap 表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34297048/
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-10-21 23:26:16  来源:igfitidea点击:

Populate HTML Bootstrap table with MySQL data using PHP

phphtmlmysqltwitter-bootstrap

提问by Pablo Quemé

I downloaded a Bootstrap template for admin panel. I want to fill a table with data from a MySQL database. I created a Stored Procedure in the database that basically makes a SELECT * FROM CLIENTS, and to test it out I made the following php file:

我下载了管理面板的 Bootstrap 模板。我想用 MySQL 数据库中的数据填充一个表。我在数据库中创建了一个存储过程,它基本上是一个 SELECT * FROM CLIENTS,为了测试它,我制作了以下 php 文件:

<?php
$conn = mysqli_connect("localhost", "user", "password", "db", "port");

$result = mysqli_query($conn, "call myStoredProcedure")
  or die("Query fail: " . mysqli_error());

while ($row = mysqli_fetch_array($result)) {
  echo $row[0] . ' - ' . $row[1] . ' - ' . $row[2] . "<br>";
}
?>

And it returns just what I want, a list of clients with some info separated by '-'. It's saved as an individual PHP file and run from my browser.

它只返回我想要的,一个客户列表,其中包含一些以“-”分隔的信息。它被保存为一个单独的 PHP 文件并在我的浏览器中运行。

Ok, so I need that data in a table. I use this to create the table:

好的,所以我需要表格中的数据。我用它来创建表:

<table class="table table-striped table-bordered table-hover" id="dataTables-example">
    <thead>
        <tr>
            <th>Code</th>
            <th>NIT</th>
            <th>Name</th>
            <th>Address</th>
            <th>Type</th>
        </tr>
    </thead>
    <?php
      $conn = mysqli_connect('localhost', 'user', 'password', 'db', 'port');
      $result = mysqli_query($conn, 'call myStoredProcedure') or die('Query fail: ' . mysqli_error());
    ?>
    <tbody>
      <?php while ($row = mysqli_fetch_array($result)) { ?>
          <tr>
            <td><?php echo $row[0]; ?></td>
            <td><?php echo $row[1]; ?></td>
            <td><?php echo $row[2]; ?></td>
            <td><?php echo $row[3]; ?></td>
            <td><?php echo $row[4]; ?></td>
          </tr>
      <?php } ?>
    </tbody>
</table>

So as you can see, I use the exact same code that I use on the PHP file, but nothing is changing on the table, it says it has 1 record but it's empty.

如您所见,我使用的代码与我在 PHP 文件上使用的代码完全相同,但表上没有任何变化,它说它有 1 条记录,但它是空的。

Table

桌子

I tried solutions found on other questions but nothing worked for me. Any ideas on what could be wrong?

我尝试了在其他问题上找到的解决方案,但没有任何效果。关于可能出什么问题的任何想法?

Note: Both the individual php file and the html are on the same folder in the server, so I believe that both are making a successful connection and therefore, retrieving the data.

注意:单个 php 文件和 html 都在服务器的同一个文件夹中,所以我相信两者都建立了成功的连接,因此检索了数据。

Thanks a lot!

非常感谢!

EDIT: Here's the stored procedure I call from the PHP:

编辑:这是我从 PHP 调用的存储过程:

BEGIN
    SELECT * FROM CLIENTS;
END

采纳答案by scaisEdge

try with this way for echo

用这种方式尝试回声

<tbody>
  <?php while ($row = mysqli_fetch_array($result)) { 
    echo "<tr>
        <td>" . $row[0] . "</td>
        <td>" . $row[1] . "</td>
        <td>" . $row[2] . "</td>
        <td>" . $row[3] . "</td>
        <td>" . $row[4] . "</td>
      </tr>"; 
  ?>
</tbody>