2011-06-27 9 views
2

11データベーステーブルのデータをXMLとしてエクスポートしようとしています。私は簡単に1つのテーブルを問題なくエクスポートすることができました。しかし、私は本当に複数を輸出しようとしています。複数のDBテーブルをSQLデータとしてエクスポートする

確かに、私は確かに別のテーブルエンティティとしてデータを出力します。どんな助けもこの1つで非常に高く評価されています。私はそれがちょっと難しいと思っています。

<?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を移転最初から始めてください。
  • 新しいルートノードが追加されました。

それはそれです。特定のデータベースからすべてのテーブルを出力したい場合は、 "show tables;" dbが構成するテーブルを調べるための-query。

<?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

パーフェクト。ありがとう。配列のループを見ていたら、あなたの答えが出てきました。 – StuBlackett

関連する問題