[英]Echo html table with specific field
我是PHP的新手。 而且我似乎在這里找不到我的代碼的問題。 我正在嘗試從表中選擇特定的行,並使用自己的標頭在HTML表中回顯它。 我一直在表消息中獲取無可用數據。
<?php
$db_host = 'localhost';
$db_user = 'my user';
$db_pwd = 'my pwd';
$database = 'my db';
$table = 'client';
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 'id', 'datecreated', 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>";
echo "<th>Client ID</th>";
echo "<th>Sign Up Date</th>";
echo "</thead>";
// printing table rows
if ($results->num_rows > 0) {
while ($row = $results->fetch_assoc()){
}
echo "<tbody>";
echo "<tr>";
echo "<td>".$row["id"]."</td>";
echo "<td>".$row["datecreated"]."</td>";
echo "</tr>";
echo "</tbody>";
}
mysql_free_result($result);
?>
將while循環周圍的代碼順序更改為:
if ($fields_num > 0) {
echo "<tbody>";
while ($row = mysql_fetch_assoc($result)) {
echo "<tr>
<td>".$row["id"]."</td>
<td>".$row["datecreated"]."</td>
</tr>";
}
echo "</tbody>";
}
目前,您實際上並沒有對要檢索的數據做任何事情。
和僅供參考-mysql已過時,您應該使用mysqli
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.