简体   繁体   中英

Populate HTML Bootstrap table with MySQL data using PHP

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:

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

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.

表

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.

Thanks a lot!

EDIT: Here's the stored procedure I call from the 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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