phpMyAdmin匯入匯出雖方便,但受限於php.ini上傳大小(預設2MB)與單引號跳脫問題,超過6MB資料庫匯入常失敗。針對10MB+資料庫,作者設計「結構用phpMyAdmin、資料用腳本」的混合方案。
CREATE TABLE)→ PHP腳本讀取資料逐行寫入文字檔。INSERT語句寫入資料庫。export.php?table=tablename)<?php
if($table=="") exit();
mysql_connect("localhost","user","pass");
mysql_select_db("database");
$result = mysql_query("SELECT * FROM $table");
if(mysql_num_rows($result) <= 0) exit();
$handle = fopen("$table.txt","w");
$num_fields = mysql_num_fields($result);
fputs($handle, $num_fields."\r\n");
while($row = mysql_fetch_row($result)) {
for($i=0; $i<$num_fields; $i++) {
$row[$i] = str_replace("\r\n", "&&PHP2000MySQLreturn&&", $row[$i]);
$row[$i] = str_replace("\n", "&&PHP2000MySQLreturn&&", $row[$i]);
fputs($handle, $row[$i]."\r\n");
}
fputs($handle, "------- PHP2000 dump data program V1.0 for MySQL --------\r\n");
}
fclose($handle);
echo "ok";
?>
特點:每欄位一行、自訂換行符標記&&PHP2000MySQLreturn&&避免單引號衝突、表尾標記分隔記錄。
import.php?table=tablename)<?php
if($table=="") exit();
mysql_connect("localhost","user","pass");
mysql_select_db("database");
$lines = file("$table.txt");
$num_fields = (int)trim($lines[0]);
for($k=1; $k
實務注意事項
file()一次讀入整檔,超大檔案建議改用fopen()+fgets()逐行處理。
- 檔案傳輸需透過FTP:匯出後下載、匯入前上傳。
- 單引號與換行符雙重跳脫機制,解決phpMyAdmin常見匯入失敗主因。
此方案以純文字檔為中介,繞過PHP上傳限制與phpMyAdmin解析缺陷,實測10MB+資料庫穩定可用,適合無Shell存取權限的虛擬主機環境。