承接上篇fdlist.php產生字段映射清單,本篇import.php讀取fdlist.txt,透過ODBC連線Access,將資料逐筆轉入MySQL,支援字段刪減與型別自動適配。
task)。fdlist.txt:來源表名、來源字段清單、目標表名、目標字段清單。WHERE 1=2查詢目標表結構,驗證字段數一致。DELETE FROM target_table清空舊資料。while (odbc_fetch_row($rs)) {
$sql = "INSERT INTO $target_table ($target_fields) VALUES (";
for ($i=1; $i<=$field_count; $i++) {
$val = odbc_result($rs, $i);
// 字串型別加單引號,數值型別直接輸出
$sql .= (mysql_field_type($mysql_rs, $i-1) == 'string') ? "'$val'," : "$val,";
}
$sql = rtrim($sql, ',') . ")";
mysql_query($sql);
}
fdlist.txt手動刪除不需遷移的字段,無需修改程式碼。數十張表、含Memo長文字欄位、四五張表需字段調整,**20分鐘全遷移完成**,完美解決Access匯出分隔符衝突與中文欄位名失效問題。
兩支腳本fdlist.php + import.php構成輕量級Access→MySQL遷移工具鏈,零授權成本、可見原始碼、可客製化,適合中小專案快速落地。