2011-06-27 57 views
2

我正在尋找將11個數據庫表的數據導出爲XML。我很容易設法導出一個表,沒有問題。但我期望真正出口多個國家。導出多個數據庫表作爲SQL數據

我確定這是一種方式,顯然將數據作爲單獨的表格實體輸出。任何幫助都非常讚賞這個,因爲我覺得它有點棘手。

我的代碼如下

<?php 

error_reporting(E_ALL); 

$host  = "localhost"; 
$user  = "root"; 
$pass  = ""; 
$database = "db_etch"; 
$table = "keywords"; 

$SQL_query = "SELECT * FROM $table"; 

$DB_link = mysql_connect($host, $user, $pass) or die("Could not connect to host."); 
mysql_select_db($database, $DB_link) or die ("Could not find or access the database."); 
$result = mysql_query ($SQL_query, $DB_link) or die ("Data not found. Your SQL query didn't work... "); 

// produce XML 
header("Content-type: text/xml"); 
$XML = "<?xml version=\"1.0\"?>\n"; 

// root node 
$XML .= "<result>\n"; 
// rows 
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {  
    $XML .= "\t<$table>\n"; 
    $i = 0; 
    // cells 
    foreach ($row as $cell) { 

    $cell = str_replace("&", "&amp;", $cell); 
    $cell = str_replace("<", "&lt;", $cell); 
    $cell = str_replace(">", "&gt;", $cell); 
    $cell = str_replace("\"", "&quot;", $cell); 
    $col_name = mysql_field_name($result,$i); 
    $XML .= "\t\t<" . $col_name . ">" . $cell . "</" . $col_name . ">\n"; 
    $i++; 
    } 
    $XML .= "\t</$table>\n"; 
} 
$XML .= "</result>\n"; 

// output the whole XML string 
echo $XML; 

// Write $sql to file 
$File = "keywords.xml"; 
$fh = fopen($File, 'w') or die("can't open file"); 
$stringData = $XML; 
fwrite($fh, $stringData); 
fclose($fh); 

?> 

回答

2

我改變了一些代碼,假設一些小的額外你想要的東西:

  • 更改str_replace()函數進行htmlspecialchars()
  • 感動XML從一開始就開始。
  • 添加了一個新的根節點。

就是這樣。如果要輸出某個數據庫的所有表格,則應使用「顯示錶​​格」; - 查詢數據庫組成的表。

<?php 

error_reporting(E_ALL); 

$host  = "localhost"; 
$user  = "root"; 
$pass  = ""; 
$database = "db_etch"; 
$table = "keywords"; 

$tables_to_output_array = array('keywords', 'othertable1', 'othertable2'); 


$DB_link = mysql_connect($host, $user, $pass) or die("Could not connect to host."); 

mysql_select_db($database, $DB_link) or die ("Could not find or access the database."); 

// produce XML 
header("Content-type: text/xml"); 
$XML = "<?xml version=\"1.0\"?>\n"; 
// root node 
$XML .= "<tables>\n"; 

while (list(, $table) = each($tables_to_output_array)) { 


    $SQL_query = "SELECT * FROM $table"; 

    $result = mysql_query ($SQL_query, $DB_link) or die ("Data not found. Your SQL query didn't work... "); 

    // tables 
    $XML .= "\t<$table>\n"; 
    // rows 
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
    $XML .= "\t\t<row>\n"; 
    $i = 0; 
    // cells 
    foreach ($row as $cell) { 
     $col_name = mysql_field_name($result,$i); 
     $XML .= "\t\t\t<" . $col_name . ">" . htmlspecialchars($cell) . "</" . $col_name . ">\n"; 
     $i++; 
    } 
    $XML .= "\t\t</row>\n"; 
    } 
    $XML .= "\t</$table>\n"; 

} 

$XML .= "</tables>\n"; 

// output the whole XML string 
echo $XML; 

// Write $sql to file 
$File = "keywords.xml"; 
$fh = fopen($File, 'w') or die("can't open file"); 
$stringData = $XML; 
fwrite($fh, $stringData); 
fclose($fh); 

?> 
+0

完美。謝謝。只是看着,while數組然後遇到你的答案,非常感謝! – StuBlackett