1

如何使用 PHP 显示数据库中的所有信息(所有表和记录)?我阅读了将表格显示为 HTML 表格,但如何为所有表格执行此操作?

我在这里尝试了这个例子:http: //davidwalsh.name/html-mysql-php

但它显示了表名,我如何也显示所有值?

4

3 回答 3

4

好的。。试试这个

<?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();
于 2013-01-31T07:10:49.190 回答
-1

您可以使用递归函数来显示数据库的树结构。

这是来自 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();
?>
于 2016-05-26T12:13:28.567 回答
-5

如果要tables使用valuesin拉取所有mysql内容,可以使用以下代码:

<?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 . "&nbsp;-&nbsp;" . 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您需要稍微调整一下。

于 2013-01-31T04:33:10.293 回答