简体   繁体   English

使用PHP使用MySQL数据填充HTML Bootstrap表

[英]Populate HTML Bootstrap table with MySQL data using PHP

I downloaded a Bootstrap template for admin panel. 我下载了用于管理面板的Bootstrap模板。 I want to fill a table with data from a MySQL database. 我想用MySQL数据库中的数据填充表。 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: 我在数据库中创建了一个存储过程,该存储过程基本上使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条记录,但是它是空的。

表

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

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM