2015-11-21 61 views
0

我有一个php代码,显示来自SQL数据库的名称和编号。 而我有一个列表名称和数字的HTML表格。我希望我的SQL数据库中的名称和编号能够显示在HTML表格中。如何让我的PHP代码,在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> 
+1

你必须'echo' HTML代码 – sinaza

回答

2

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> 
+0

我需要指定数据库细节打造连接? –

+0

它正在工作:) Yay –

+0

但是。它显示最后的最旧的和最新的。链接= http://whatscast.in/test.php。所以,现在我想要显示较新的第一个和最后一个在最后.. –

相关问题