<?php
$conn = mysql_connect('localhost', 'root', 'pass');
mysql_select_db('embed', $conn);
$tables = mysql_list_tables('embed', $conn);
for ($i = 0; $i < mysql_num_rows($tables); $i++) {
$name = mysql_tablename($tables, $i);
echo "<a href='Backup.php?table=$name'>$name</a><br>";
}
?>
<?php
if (empty($_GET['table'])) header('Location: Listtable.php');
$table = $_GET['table'];
$conn = mysql_connect('localhost', 'root', 'pass');
mysql_select_db('embed', $conn);
$res = mysql_query("SELECT * FROM `$table`", $conn);
$fields = [];
$num = mysql_num_fields($res);
for ($i = 0; $i < $num; $i++) $fields[] = mysql_field_name($res, $i);
$xml = '';
$xml .= "<{$table}s>";
while ($row = mysql_fetch_assoc($res)) {
$xml .= "<$table>";
foreach ($fields as $f) {
$xml .= "<$f>" . htmlspecialchars($row[$f]) . "$f>";
}
$xml .= "$table>";
}
$xml .= "{$table}s>";
$file = $table . '.xml';
file_put_contents($file, $xml);
echo "資料表 $table 備份成功!";
?>
<欄位名>值</欄位名>,還原時可直接對應htmlspecialchars() 防止 < > & 破壞 XML 結構encoding="GB2312" 或 UTF-8,需與資料庫實際編碼一致LIMIT offset, 1000 分批寫入,避免記憶體溢位解析 XML → SimpleXML 或 XMLReader 逐行讀取 → 組裝 INSERT 或 REPLACE → 事務執行。大檔案請用 XMLReader 流式解析。
mysqldump --xml -u root -p db table > table.xml (官方工具,效能最強)本文方案適合「需自定義 XML 結構、嵌入式環境無法安裝客戶端工具」的特殊場景。