0

我正在创建一个将与数据库连接的网站。我在 SQL Server 中创建了一个数据库。

我想将一个表格与一个 html 页面连接起来,以便我可以查看该页面上的表格。通过这种方式,我会将所有 SQL 表与每个 html 页面连接起来。

我知道我必须使用 php/javaquery!

请帮助我提供适当的代码!

我也是 SQL 新手。我在学习!

请解释我必须在您提供的代码中进行哪些更改才能在我的系统中启用它。

我得到了这两个代码:这将满足我的要求?代码1:

  $result = mysqli_query($con,"SELECT * FROM Orders");

 echo "<table>";
 echo "<table border='1'>
 <tr>
 <th>ID</th>
 <th>orderNumber</th>
 <th>Price</th>
 <th>customerName</th>
 <th>salesRep</th>
 <th>DatePicker</th>
 <th>shipMethod</th>
 <th>trackingNumber</th>
 <th>Statuscheck</th>
 <th>Edit</th>
 </tr>";

 while($row = mysqli_fetch_array($result))
   {
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['orderNumber'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "<td>" . $row['customerName'] . "</td>";
echo "<td>" . $row['salesRep'] . "</td>";
echo "<td>" . $row['DatePicker'] . "</td>";
echo "<td>" . $row['shipMethod'] . "</td>";
echo "<td>" . $row['trackingNumber'] . "</td>";
echo "<td>" . $row['Statuscheck'] . "</td>";
echo "<td>" . $row['Edit'] . "</td>";
echo "</tr>";
  }
echo "</table>";

?>

代码2:

 <?php

   $host=""; // Host name 
   $username=""; // Mysql username 
   $password=""; // Mysql password 
   $db_name=""; // Database name 
   $tbl_name=""; // Table name 

  // Connect to server and select databse.
     mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
     mysql_select_db("$db_name")or die("cannot select DB");
     $sql="SELECT * FROM $tbl_name ORDER BY id DESC";
  // OREDER BY id DESC is order result by descending
  }
      $result=mysql_query($sql);

    ?>

  <html>

  <head>

   <script type="text/javascript" charset="utf-8" src="jquery.js"></script>

  </head>

   <table id="forum" width="90%" border="0" align="center"          cellpadding="3"      cellspacing="1" bgcolor="#CCCCCC">

 <thead>

  <tr>
   <th width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
    <th width="43%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
   <th width="10%" align="center" bgcolor="#E6E6E6"><strong>Author</strong></td>
   <th width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
    <th width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
     <th width="13%" align="center"  bgcolor="#E6E6E6"><strong>Date/Time</strong></td>

     </tr>

    </thead>

    <?php 
  // Start looping table row
 while($rows=mysql_fetch_array($result)){
     ?>

  <tbody>

  <tr>
  <td bgcolor="#FFFFFF"><? echo $rows['threadtype']; ?></td>
  <td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>">
  <? echo $rows['topic']; ?></a><BR></td>
 <td align="center" bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
  <td align="center" bgcolor="#FFFFFF"><? echo $rows['view']; ?></td>
   <td align="center" bgcolor="#FFFFFF"><? echo $rows['reply']; ?></td>
   <td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
  </tr>

  </tbody>

   <?php

   // Exit looping and close connection 
  }
   mysql_close();

  ?>

 <tfoot>
 <tr>
 <td colspan="6" align="right" bgcolor="#E6E6E6"><strong>Create New  Topic</strong> td>
</tr>
  </tfoot>
</table>

 </html>
4

1 回答 1

1

如果我正确理解了您的问题,您希望显示从数据库中检索到的数据。我不是php专家,但根据代码,让我尝试解释一下。

1)首先使用提供的连接字符串连接到数据库并获取数据

     <?php

   $host=""; // Host name 
   $username=""; // Mysql username 
   $password=""; // Mysql password 
   $db_name=""; // Database name 
   $tbl_name=""; // Table name 

  // Connect to server and select databse.
     mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
     mysql_select_db("$db_name")or die("cannot select DB");
     $sql="SELECT * FROM $tbl_name ORDER BY id DESC";
  // OREDER BY id DESC is order result by descending
  }
      $result=mysql_query($sql);

    ?>

2) 创建一个 HTML 表格来显示数据。(首先创建表头)

<table id="forum" width="90%" border="0" align="center"          cellpadding="3"      cellspacing="1" bgcolor="#CCCCCC">

 <thead>

  <tr>
   <th width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
    <th width="43%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
   <th width="10%" align="center" bgcolor="#E6E6E6"><strong>Author</strong></td>
   <th width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
    <th width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
     <th width="13%" align="center"  bgcolor="#E6E6E6"><strong>Date/Time</strong></td>

     </tr>

    </thead>

3) 现在将php代码返回的数据动态添加到这个HTML表格中

while($row = mysqli_fetch_array($result))
   {
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['orderNumber'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "<td>" . $row['customerName'] . "</td>";
echo "<td>" . $row['salesRep'] . "</td>";
echo "<td>" . $row['DatePicker'] . "</td>";
echo "<td>" . $row['shipMethod'] . "</td>";
echo "<td>" . $row['trackingNumber'] . "</td>";
echo "<td>" . $row['Statuscheck'] . "</td>";
echo "<td>" . $row['Edit'] . "</td>";
echo "</tr>";
  }
echo "</table>";

?>

4) 完成 HTML 表格

<tfoot>
 <tr>
 <td colspan="6" align="right" bgcolor="#E6E6E6"><strong>Create New  Topic</strong> td>
</tr>
  </tfoot>
</table>

5)最后作为最佳实践关闭与数据库的连接以避免任何不必要的问题+性能提升。

<?php

   // Exit looping and close connection 
  }
   mysql_close();

  ?>
于 2016-06-28T12:28:37.307 回答