入門驅動通過這一個瘋狂...Drupal的6分貝插入:字符串越來越逃脫
我試圖插入的行數到D6數據庫與單db_query電話。我收集的行爲一組字符串,並然後收集它們變成一個大的字符串,像這樣:
$theData = "(1, 2, 'a'), (3, 4, 'b'), (5, 6, 'c')";
db_query("insert into {table} (int1, int2, str) values %s", $theData);
($海圖的類型不能像在我的代碼;它的結果我已經寫了代碼 - 在運行此含套括號包裹值的大串)
,我得到這樣一個錯誤:
You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 'insert into
table (int1, int2, str) values (1,2,\'a\' at line 1 query: insert into
table (int1, int2, str) values (1,2,\'a\'),(3,4,\'n\'),(5,6,\'c\')...
所以,db_query或有其他人逃離將值傳遞給mysql之前的字符串。我如何避免這種情況發生?我可以爲每組數據做單獨的查詢,但是由於所有顯而易見的原因,這是錯誤的/昂貴的。謝謝!
新的發現:我可以做到這一點 - db_query(「insert into {table}(int1,int2,str)values」。$ theData) - 但它感覺有點俗氣。有什麼更好的嗎? – 2011-03-23 22:46:55
這兩種方式都是錯誤的和不安全的,請使用Nikit建議的內容。 Drupal 7支持在單個查詢中插入多行,請參閱http://drupal.org/node/310079 – Berdir 2011-03-23 23:25:02