[英]Echo html table with specific field
I am fairly new at PHP. 我是PHP的新手。 And I can't seem to find the issue with my code here. 而且我似乎在这里找不到我的代码的问题。 I am trying to SELECT specific rows from my table and echo it in my HTML table using my own headers. 我正在尝试从表中选择特定的行,并使用自己的标头在HTML表中回显它。 I keep getting No data available in table message. 我一直在表消息中获取无可用数据。
<?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);
?>
Change the order your code around the while loop to: 将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>";
}
At the moment you aren't actually doing anything with the data you're retrieving. 目前,您实际上并没有对要检索的数据做任何事情。
And FYI - mysql is deprecated, you should use mysqli 和仅供参考-mysql已过时,您应该使用mysqli
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.