Php 如何为html使用数据库表头<;th>;表头标签

Php 如何为html使用数据库表头<;th>;表头标签,php,html,mysql,html-table,Php,Html,Mysql,Html Table,我是PHP新手。我使用下面的代码将MySQL表打印为HTML表 但是,我的代码从数据库打印表头 如何使用标题标记以硬编码格式打印HTML表格标题并打印表格中的所有行? 谢谢 <?php $db_host = 'localhost'; $db_user = 'my user'; $db_pwd = 'my pwd'; $database = 'my db'; $table = 'subcontractor'; if (!mysql_connect($db_host, $db_

我是PHP新手。我使用下面的代码将MySQL表打印为HTML表

但是,我的代码从数据库打印表头

如何使用
标题
标记以硬编码格式打印HTML表格标题并打印表格中的所有行?

谢谢

<?php
 $db_host = 'localhost';
 $db_user = 'my user';
 $db_pwd = 'my pwd';

 $database = 'my db';
 $table = 'subcontractor';

 if (!mysql_connect($db_host, $db_user, $db_pwd))
     die("Can't connect to database");

 if (!mysql_select_db($database))
     die("Can't select database");

 // sending query
 $result = mysql_query("SELECT * FROM {$table}");
 if (!$result) {
     die("Query to show fields from table failed");
 }

 $fields_num = mysql_num_fields($result);

 echo "<table class='table table-bordered table-striped mb-none' id='datatable-tabletools' data-swf-path='assets/vendor/jquery-datatables/extras/TableTools/swf/copy_csv_xls_pdf.swf' >";

 // printing table headers
 echo "<thead>";
 for($i=0; $i<$fields_num; $i++)
 {
     $field = mysql_fetch_field($result);
     echo "<th>{$field->name}</th>";
 }
 echo "</thead>";
 // printing table rows
 while($row = mysql_fetch_row($result))
 {
     echo "<tbody>";
     echo "<tr>";
     echo "</thead>";

     // $row is array... foreach( .. ) puts every element
     // of $row to $cell variable
     foreach($row as $cell)
         echo "<td>$cell</td>";

     echo "</tr>";
     echo "</tbody>";
 }
 mysql_free_result($result);
 ?>

您可以通过删除这些代码行来更改标题

for($i=0; $i<$fields_num; $i++)
{   
   $field = mysql_fetch_field($result);
   echo "<th>{$field->name}</th>";
}
$i=0;$i的

谢谢,非常好。有没有办法让PHP跳过数据库表中的一列?谢谢——

是的,只需更改查询即可

$result = mysql_query("SELECT * FROM {$table}");
例如

$result = mysql_query("SELECT `name`,`email`,`address` FROM {$table}");

因此,请删除数据库输出,并回复“您想要的文本”
$result = mysql_query("SELECT `name`,`email`,`address` FROM {$table}");