【问题标题】:How to make my PHP code, display in html table?如何使我的 PHP 代码显示在 html 表格中?
【发布时间】:2015-11-21 20:29:41
【问题描述】:

我有一个显示 SQL 数据库中的名称和编号的 php 代码。 我有一个包含名称和编号列的 html 表。我希望我的 SQL 数据库中的名称和编号显示在 HTML 表中。

PHP 代码

<?php
$servername = "localhost";
$username = "what7aba_wc";
$password = "This is my Password";
$dbname = "what7aba_wc";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT name, number FROM WC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Name" . $row["name"]. "-Number" . $row["number"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

HTML 表格

  <table id="table" class="table table-hover table-mc-light-blue">
      <thead>
        <tr>
          <th>Name</th>
          <th>Link</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td data-title="Name">Its a Name</td>
          <td data-title="Number">

        </tr>
        <tr>
          <td data-title="Name">Its a Name</td>
          <td data-title="Number">

        </tr>
      </tbody>
    </table>

如果这不能发生。任何人都告诉我如何为上面给出的 PHP 文件制作 CSS。

更新:

我已经试过了。

<?php
$servername = "localhost";
$username = "what7aba_wc";
$password = "My Password";
$dbname = "what7aba_wc";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT name, number FROM WC";
$result = $conn->query($sql);

?>
  <h1>WhatsCast List</h1>
  <h2>Table of my other Material Design works (list was updated 08.2015)</h2>

  <div class="table-responsive-vertical shadow-z-1">
  <table id="table" class="table table-hover table-mc-light-blue">
      <thead>
        <tr>
          <th>Name</th>
          <th>Link</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td data-title="Name"><p><?php echo $row["name"] ?><p></td>
          <td data-title="Number"><p><?php echo $row["number"] ?></p></td>
         </tr>

      </tbody>
    </table>
  </div>

</div>

【问题讨论】:

  • 你必须echo HTML 代码

标签: php html mysql css


【解决方案1】:

HTML

<table id="table" class="table table-hover table-mc-light-blue">
  <thead>
    <tr>
      <th>Name</th>
      <th>Link</th>
    </tr>
  </thead>
  <tbody>
    <?php
      while($row = $result->fetch_assoc()) { ?>
     <tr>
      <td data-title="Name"><?php echo $row["name"]?></td>
      <td data-title="Number"><?php echo $row["number"] ?></td>
    </tr>
    <? } ?>
  </tbody>
</table>

【讨论】:

  • 是否需要指定数据库详细信息才能创建连接?
  • 但是。它在最后显示最旧的第一个和较新的。链接=whatscast.in/test.php。所以,现在我希望它首先显示较新的,最后显示较旧的..
  • 这是您的示例页面吗? - whatscast.in/test.php我可以看看你的最新代码吗?
猜你喜欢
  • 2018-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-03
  • 1970-01-01
  • 2016-01-24
相关资源
最近更新 更多