如何在html表

时间:2017-01-14 21:30:28

标签: php mysql

我正在使用以下代码在html表中显示MySQL表中的行。我已经在php中定义了表头 - 请问任何人都可以建议我如何修改代码以阻止它将MySQL列标题引入表中?

目前,在记录开始显示在第3行之前,它显示表格标题(第1行),然后显示MySQL列标题(第2行)。

我尝试在代码中的各个点使用-N来抑制列标题,但它只会引发错误。

<?php

// Create connection
$con = mysql_connect("localhost","xxxxxxxxxx","xxxxxxxxx");
// Check connection
if (!$con) {
    die("Connection failed: " . mysql_error());
} 

mysql_select_db("xxxxxxxxxxxx",$con);
$sql = "SELECT * FROM SiteList";
$myData = mysql_query($sql,$con);
echo "<table border=1>
<tr>
<th>ID</th>
<th>Site_Name</th>
<th>Weather_Forecast</th>
<th>Forecast_Date</th>
<th>Forecast_Time</th>
<th>Further_Information_about_This_Site</th>
</tr>";
while($record = mysql_fetch_array($myData)) {
echo "<tr>";
echo "<td>" . $record['ID'] . "</td>";
echo "<td>" . $record['Site_Name'] . "</td>";
echo "<td>" . $record['Weather_Forecast'] . "</td>";
echo "<td>" . $record['Forecast_Date'] . "</td>";
echo "<td>" . $record['Forecast_Time'] . "</td>";
echo "<td>" . $record['Further_Information_about_This_Site'] . "</td>";
echo "</tr>";
}

echo "</table>";

mysql_close($con);

?>

1 个答案:

答案 0 :(得分:1)

您正在循环检索数据库中检索到的数据。因此记录必须存在于数据库中。如果您仍想忽略数据

$skip = 0; // declare coutner
while($record = mysql_fetch_array($myData)) {

if ($skip != 0){ // skip on first loop
echo "<tr>";
echo "<td>" . $record['ID'] . "</td>";
echo "<td>" . $record['Site_Name'] . "</td>";
echo "<td>" . $record['Weather_Forecast'] . "</td>";
echo "<td>" . $record['Forecast_Date'] . "</td>";
echo "<td>" . $record['Forecast_Time'] . "</td>";
echo "<td>" . $record['Further_Information_about_This_Site'] . "</td>";
echo "</tr>";
 }

$skip++; // increment counter

}