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("&", "&", $cell);
$cell = str_replace("<", "<", $cell);
$cell = str_replace(">", ">", $cell);
$cell = str_replace("\"", """, $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);
?>
完美。謝謝。只是看着,while數組然後遇到你的答案,非常感謝! – StuBlackett