2011-06-06 56 views
2

是否有可能通過PHP讀取數據庫而不知道字段名稱?所以它會是這樣的:如何在不知道域名的情況下加載整個數據庫?

1.獲取表「myTable」的結果數組。

2.計算每行內有多少個字段。

3.讓循環創建html表並回顯值。

通常我們知道表格的字段名稱,所以它很容易閱讀。如果你不知道字段名稱怎麼辦?

謝謝。

回答

1

這應該做的伎倆:

$res = mysql_query("SELECT * FROM MyTable"); 
$rows_count = mysql_num_rows($res); 
echo '<table>'; 
for($i=0; $i<$rows_count; $i++) 
{ 
    echo '<tr>'; 
    $row = mysql_fetch_row($res); 
    for($r=0;$r<count($row);$r++) 
    { 
     echo '<td>'; 
     echo $row[$r]; 
     echo '</td>'; 
    } 
    echo '</tr>'; 
} 
echo '</table>'; 
4

使用*目標的所有領域:

select * from myTable 

如果你有超過1臺,你可以用表的名稱前綴*

select 
    another_table.* 
from 
    my_table 
    left join another_table on another_table.id = my_table.another_table_id; 

或者,你可以使用show fields in myTableshow full fields in myTable到只獲取字段列表(沒有數據)。

1

,您可以:

SELECT `COLUMN_NAME`, `TABLE_NAME` 
FROM information_schema.`COLUMNS` 
1

這裏的快速和骯髒的腳本來獲取整個表的數據,而不知道它的領域:

<?php 

$conn = mysql_connect('localhost', 'root', ''); 

$sql = "SELECT * FROM `mysql`.`tables_priv`"; 
$rs = mysql_query($sql); 
$tableText = "<table></table>"; 
$tableHeader = array(); 
$tableContent = ''; 
$tableHeaderSet = false; 
while(false !== ($r = mysql_fetch_assoc($rs))) 
{ 
    if(false == $tableHeaderSet) 
    { 
     $tableHeaderText = "<tr>"; 
     foreach($r as $key=>$val) 
     { 
      $tableHeader[$key] = $key; 
      $tableHeaderText .= "<th>$key</th>"; 
     } 
     $tableHeaderText .= "</tr>"; 

    } 
    $tableHeaderSet = true; 
    $tableContent .= "<tr>"; 
    foreach($tableHeader as $fieldName) 
    { 
     $tableContent .= "<td>" . $r[ $fieldName ] . "</td>"; 
    } 
    $tableContent .= "</tr>"; 

} 

echo "<table>{$tableHeaderText}{$tableContent}</table>"; 
?> 
2
<?php 

//connection variables 
$host = ""; 
$database = ""; 
$user = ""; 
$pass = ""; 

//connection to the database 
mysql_connect($host, $user, $pass) 
or die ('cannot connect to the database: ' . mysql_error()); 

//select the database 
mysql_select_db($database) 
or die ('cannot select database: ' . mysql_error()); 

//loop to show all the tables and fields 
$loop = mysql_query("SHOW tables FROM $database") 
or die ('cannot select tables'); 

while($row = mysql_fetch_array($loop)) 
{ 

echo " 
<table cellpadding=2 cellspacing=2 border=0 width=75%> 
<tr bgcolor=#666666> 
<td colspan=5><center><b><font color=#FFFFFF>」 . $row[0] . 「</font></center></td> 
</tr> 
<tr> 
<td>Field</td><td>Type</td><td>Key</td><td>Default</td><td>Extra</td> 
</tr>"; 

$i = 0; 

$loop2 = mysql_query("SHOW columns FROM " . $row[0]) 
or die ('cannot select table fields'); 

while ($row2 = mysql_fetch_array($loop2)) 
{ 
echo "<tr "; 
if ($i % 2 == 0) 
echo "bgcolor=#CCCCCC"; 
echo "><td>" . $row2[0] . "</td><td>" . $row2[1] . "</td><td>" . $row2[2] . "</td><td>" . $row2[3] . "</td><td>" . $row2[4] . "</td></tr>"; 
$i++; 
} 
echo "</table><br/><br/>"; 

} 
?> 

來源:http://jadendreamer.wordpress.com/2009/0 1/13/print-all-mysql-database-tables-fields-using-php/

0

您可以使用系統表。例如,在Oracle DB中,表ALL_TAB_COLUMNS包含有關用戶表,視圖和集羣列的信息。

相關問題