【发布时间】:2013-01-31 04:07:59
【问题描述】:
如何使用 PHP 显示数据库中的所有信息(所有表和记录)?我读到将表格显示为 HTML 表格,但如何为所有表格执行此操作?
我在这里尝试了这个例子: http://davidwalsh.name/html-mysql-php
但它显示了表名,我如何同时显示所有值?
【问题讨论】:
如何使用 PHP 显示数据库中的所有信息(所有表和记录)?我读到将表格显示为 HTML 表格,但如何为所有表格执行此操作?
我在这里尝试了这个例子: http://davidwalsh.name/html-mysql-php
但它显示了表名,我如何同时显示所有值?
【问题讨论】:
好的..试试这个
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "username", "password", "DB");
$result = $mysqli->query("SHOW TABLES");
while ($row = $result->fetch_row()) {
$table = $row[0];
echo '<h3>', $table, '</h3>';
$result1 = $mysqli->query("SELECT * FROM `$table`");
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
$column = $mysqli->query("SHOW COLUMNS FROM `$table`");
echo '<tr>';
while ($row3 = $column->fetch_row()) {
echo '<th>' . $row3[0] . '</th>';
}
echo '</tr>';
while ($row2 = $result1->fetch_row()) {
echo '<tr>';
foreach ($row2 as $key => $value) {
echo '<td>', $value, '</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
$mysqli->close();
【讨论】:
您可以使用递归函数来显示您的数据库的树状结构。
这是来自http://webcodingeasy.com/PHP/Simple-recursive-function-to-print-out-database-tree-structure的示例代码
<?php
function print_menu($id = 0)
{
// get all records from database whose parent is $id
$result = $query->select_result("*", "table", "where parent = '".$id."'");
//check if there are any results
if($result != 0)
{
echo "<ul> n";
while($row = $query->fetch($result))
{
//print result and call function to check if it has children
echo "<li>".$row['name']."</li> n";
print_menu($row['ID']);
}
echo "</ul> n";
}
}
print_menu();
?>
【讨论】:
如果你想用values中的values拉取所有tablesmysql,可以使用如下代码:
<?php
mysql_connect ("localhost", "DB_USER", "DB_PASSWORD"); //your mysql connection
mysql_select_db('DB_NAME') or die( "Unable to select database"); //your db name
$tables = mysql_query("SELECT table_name FROM information_schema.tables WHERE table_schema='DB_NAME'"); //pull tables from theh databsase
while ($table= mysql_fetch_row($tables)) {
$rsFields = mysql_query("SHOW COLUMNS FROM ".$table[0]);
while ($field = mysql_fetch_assoc($rsFields)) {
echo $table[0].".".$field["Field"].", "; //prints out all columns
}
echo $table[0];
$query = "SELECT * FROM ".$table[0]; //prints out tables name
$result = mysql_query($query);
PullValues($result); //call function to get all values
}
//function to pull all values from tables
function PullValues($result)
{
if($result == 0)
{
echo "<b>Error ".mysql_errno().": ".mysql_error()."</b>";
}
elseif (@mysql_num_rows($result) == 0)
{
echo("<b>Query completed. No results returned.</b><br>");
}
else
{
echo "<table border='1'>
<thead>
<tr><th>[Num]</th>";
for($i = 0;$i < mysql_num_fields($result);$i++)
{
echo "<th>" . $i . " - " . mysql_field_name($result, $i) . "</th>";
}
echo " </tr>
</thead>
<tbody>";
for ($i = 0; $i < mysql_num_rows($result); $i++)
{
echo "<tr><td>[$i]</td>";
$row = mysql_fetch_row($result);
for($j = 0;$j < mysql_num_fields($result);$j++)
{
echo("<td>" . $row[$j] . "</td>");
}
echo "</tr>";
}
echo "</tbody>
</table>";
} //end else
}
?>
我正在使用它并且在mysql 中运行良好,对于mysqli,您需要稍微调整一下。
【讨论】:
mysql_query,而不是mysqli。
mysql_query 是危险的,已弃用,并将在未来的 PHP 版本中删除。请停止将其用于任何新事物。 mysql 标签一般用于 MySQL,与 PHP 中的 mysql_query 无关。
mysqli。它基本上是新的基线。