我是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);
?>
答案 0 :(得分:1)
将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>";
}
目前,您实际上并没有对要检索的数据做任何事情。
和FYI - mysql已被弃用,你应该使用mysqli