我有一個數組有30000個條目,需要進入一個MySQL表。php + mysql:插入一個php數組到mysql
最佳做法是什麼?從這裏?假設數據庫中的[0],[1]和[2]是'title','type'和'customer'
它是否添加與表中列名稱匹配的鍵名稱並調用som「magic 「功能?或建立手動查詢...
Array
(
[0] => Array
(
[0] => 2140395946
[1] => 1SAP
[2] => 0041451463
)
[1] => Array
(
[0] => 2140411607
[1] => 2SAP
[2] => 0041411940
)
[2] => Array
(
[0] => 2140706194
[1] => 4SAP
[2] => 0041411943
)
etc. etc.
更新 - 基於答案
感謝您的答案。
的解決方案通常是手動創建的SQL字符串和所有行可插入的一個:
INSERT INTO `tx_opengate_table` (`machine` ,`customer` ,`type`)
VALUES
('m123', 'dfkj45', 'A'),
('m137', 'kfkj49', 'A'), "repeat this line for each entry in the array"
... ... ...
('m654321', '34dgf456', 'C4') "end with out comma, or remove last comma"
;
特供TYPO3
我碰巧做到這一點的CMS TYPO3而只是遇到了一個新的功能添加不是很久以前:
//Insert new rows
$table = 'tx_opengate_stuff';
$fields = array('machine','type','customer');
$lines = "array as given above"
$GLOBALS['TYPO3_DB']->exec_INSERTmultipleRows($table,$fields,$lines);
超級。這就對了。我想它應該是$ item [0],$ item [1]和$ item [2],而不是$ arr數組,它應該包含$ query字符串。 – Tillebeck 2010-10-28 07:02:21
對,對不起。我修好了它。 – 2010-10-28 15:54:55
正如您在最近的評論中所述,它總是必要的:) – 2011-01-27 18:18:26