PHP MYSQLi显示数据库中的所有表

时间:2013-01-31 04:07:59

标签: php mysql mysqli

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

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

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

提前致谢

3 个答案:

答案 0 :(得分:2)

好的,试试这个

<?php
$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");
if($result1) {
    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();
?>

答案 1 :(得分:-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();
?>

答案 2 :(得分:-5)

如果您想使用tables中的values提取所有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,您需要稍微调整一下。

希望这会有所帮助。