如何显示整个 MySQL table 而无需在 PHP 中输入 table 名称
How to display entire MySQL table without entering table name in PHP
我的代码如下。其中我只能打印 table 标题。我希望我在数据库中输入其名称的 table 应该显示整个 table.
<?php
$dataconn = mysqli_connect("localhost", "root", "", "databasename");
$sql = "SELECT * FROM employeedetails";
$res = mysqli_query($dataconn, $sql);
$show = mysqli_fetch_all($res, MYSQLI_ASSOC);
$col = $show[0];
$columns = array();
echo "<pre>";
foreach ($col as $key => $value) {
if (is_string($key)) {
$columns[] = $key;
}
}
echo "<table border='1' cellpadding='10'>";
foreach ($columns as $value) {
echo "<th>$value</th>";
}
for ($x = 0; $x < count($show); $x++) {
echo "$value";
}
在第二个 foreach 循环中,您正在打印用于列名的相同变量,因此它也为第二个循环打印列名,请检查下面编辑的代码,以打印您需要的 table 数据打印查询结果
<?php
$dataconn = mysqli_connect("localhost", "root", "", "database");
$sql = "SELECT * FROM tablename";
$res = mysqli_query($dataconn, $sql);
$show = mysqli_fetch_all($res, MYSQLI_ASSOC);
$col = $show[0];
$columns = array();
echo "<pre>";
foreach ($col as $key => $value) {
if (is_string($key)) {
$columns[] = $key;
}
}
echo "<table border='1' cellpadding='10'>";
echo "<tr>";
foreach ($columns as $value) {
echo "<td>$value</td>";
}
echo "</tr>";
foreach ($show as $tableData) {
echo "<tr>";
foreach ($tableData as $key => $val) {
echo "<td>$val</td>";
}
echo "</tr>";
}
您的 for
循环不会遍历包含数据的数组。同一个值只显示N次。
您用 header 显示 HTML table 的方法有点太复杂了。 MySQLi 有一个名为 fetch_fields()
的特殊函数,它将为您提供有关结果中列的元数据。您可以使用它来显示 header 行。
echo '<table>';
// Display table header
echo '<thead>';
echo '<tr>';
foreach ($res->fetch_fields() as $column) {
echo '<th>'.htmlspecialchars($column->name).'</th>';
}
echo '</tr>';
echo '</thead>';
如果结果集也包含实际数据,那么您可以用数据迭代数组并逐行显示。
$show = mysqli_fetch_all($res, MYSQLI_ASSOC);
// If there is data then display each row
if ($show) {
foreach ($show as $row) {
echo '<tr>';
foreach ($row as $cell) {
echo '<td>'.htmlspecialchars($cell).'</td>';
}
echo '</tr>';
}
}
echo '</table>';
我的代码如下。其中我只能打印 table 标题。我希望我在数据库中输入其名称的 table 应该显示整个 table.
<?php
$dataconn = mysqli_connect("localhost", "root", "", "databasename");
$sql = "SELECT * FROM employeedetails";
$res = mysqli_query($dataconn, $sql);
$show = mysqli_fetch_all($res, MYSQLI_ASSOC);
$col = $show[0];
$columns = array();
echo "<pre>";
foreach ($col as $key => $value) {
if (is_string($key)) {
$columns[] = $key;
}
}
echo "<table border='1' cellpadding='10'>";
foreach ($columns as $value) {
echo "<th>$value</th>";
}
for ($x = 0; $x < count($show); $x++) {
echo "$value";
}
在第二个 foreach 循环中,您正在打印用于列名的相同变量,因此它也为第二个循环打印列名,请检查下面编辑的代码,以打印您需要的 table 数据打印查询结果
<?php
$dataconn = mysqli_connect("localhost", "root", "", "database");
$sql = "SELECT * FROM tablename";
$res = mysqli_query($dataconn, $sql);
$show = mysqli_fetch_all($res, MYSQLI_ASSOC);
$col = $show[0];
$columns = array();
echo "<pre>";
foreach ($col as $key => $value) {
if (is_string($key)) {
$columns[] = $key;
}
}
echo "<table border='1' cellpadding='10'>";
echo "<tr>";
foreach ($columns as $value) {
echo "<td>$value</td>";
}
echo "</tr>";
foreach ($show as $tableData) {
echo "<tr>";
foreach ($tableData as $key => $val) {
echo "<td>$val</td>";
}
echo "</tr>";
}
您的 for
循环不会遍历包含数据的数组。同一个值只显示N次。
您用 header 显示 HTML table 的方法有点太复杂了。 MySQLi 有一个名为 fetch_fields()
的特殊函数,它将为您提供有关结果中列的元数据。您可以使用它来显示 header 行。
echo '<table>';
// Display table header
echo '<thead>';
echo '<tr>';
foreach ($res->fetch_fields() as $column) {
echo '<th>'.htmlspecialchars($column->name).'</th>';
}
echo '</tr>';
echo '</thead>';
如果结果集也包含实际数据,那么您可以用数据迭代数组并逐行显示。
$show = mysqli_fetch_all($res, MYSQLI_ASSOC);
// If there is data then display each row
if ($show) {
foreach ($show as $row) {
echo '<tr>';
foreach ($row as $cell) {
echo '<td>'.htmlspecialchars($cell).'</td>';
}
echo '</tr>';
}
}
echo '</table>';