【发布时间】:2016-11-17 04:20:41
【问题描述】:
我已经设置了 wamp 服务器。我已经在数据库测试下上传了一个名为 stud 的表到 phpMyAdmin。现在我想在 Web 浏览器中将其显示为以行作为分隔符的表格。我将以下代码编写为 PHP。当我尝试运行localhost/hw.php时,报错如下:
<html>
<head>
<title>mysqli Table Viewer</title>
</head>
<body>
<?php
$db_host = 'localhost';
$database = 'test';
$table = 'stud';
if (!mysqli_select_db($database))
die("Can't select database");
// sending query
$result = mysqli_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysqli_num_fields($result);
echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysqli_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysqli_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysqli_free_result($result);
?>
</body>
</html>
如何解决这个错误?
【问题讨论】:
-
您永远不会执行到数据库的连接。
标签: php html mysqli html-table