将长数组插入INSERT查询的更简单方法?

时间:2009-09-25 12:26:55

标签: sql arrays insert

我正在尝试将此数组插入数据库中:

Array ( [0] => 1 [1] => 2376 [2] => 1804229942 53 [3] => 99 [4] => 120510105 5525 [5] => 99 [6] => 21581237 622 [7] => 99 [8] => 46612404 3 [9] => 99 [10] => 174284497 8 [11] => 99 [12] => 200000000 922 [13] => 99 [14] => 13641206 5 [15] => 99 [16] => 118438707 15 [17] => 99 [18] => 200000000 23333 [19] => 99 [20] => 13444999 9 [21] => 99 [22] => 200000000 17591 [23] => 99 [24] => 13067574 7 [25] => 99 [26] => 200000000 2 [27] => 99 [28] => 200000000 140 [29] => 99 [30] => 17959240 2087 [31] => 99 [32] => 13272978 9 [33] => 99 [34] => 53265829 2236 [35] => 99 [36] => 13194322 101 [37] => 99 [38] => 43703220 48 [39] => 99 [40] => 38083976 47 [41] => 99 [42] => 38458116 184 [43] => 99 [44] => 22217059 5267 [45] => 99 [46] => 13396733 101 [47] => 99 [48] => 15724235 903 [49] => 99 [50] => 13373505 -1 [51] => -1 -1 [52] => -1 -1 [53] => -1 -1 [54] => -1 -1 [55] => -1 ) 

我有所有相应的字段,但我的查询将是巨大的。我想知道是否有一些技巧可以绕过必须键入$ array [1],$ array [2]等等75x。

mysql_query("INSERT INTO `gotskills_clantrack`.`track_users` (`id`, `user_id`, `clan_id`, `date`, `skill1rank`, `skill1lvl`, `skill1xp`, `skill2rank`, `skill2lvl`, `skill2xp`, `skill3rank`, `skill3lvl`, `skill3xp`, `skill4rank`, `skill4lvl`, `skill4xp`, `skill5rank`, `skill5lvl`, `skill5xp`, `skill6rank`, `skill6lvl`, `skill6xp`, `skill7rank`, `skill7lvl`, `skill7xp`, `skill8rank`, `skill8lvl`, `skill8xp`, `skill9rank`, `skill9lvl`, `skill9xp`, `skill10rank`, `skill10lvl`, `skill10xp`, `skill11rank`, `skill11lvl`, `skill11xp`, `skill12rank`, `skill12lvl`, `skill12xp`, `skill13rank`, `skill13lvl`, `skill13xp`, `skill14rank`, `skill14lvl`, `skill14xp`, `skill15rank`, `skill15lvl`, `skill15xp`, `skill16rank`, `skill16lvl`, `skill16xp`, `skill17rank`, `skill17lvl`, `skill17xp`, `skill18rank`, `skill18lvl`, `skill18xp`, `skill19rank`, `skill19lvl`, `skill19xp`, `skill20rank`, `skill20lvl`, `skill20xp`, `skill21rank`, `skill21lvl`, `skill21xp`, `skill22rank`, `skill22lvl`, `skill22xp`, `skill23rank`, `skill23lvl`, `skill23xp`, `skill24rank`, `skill24lvl`, `skill24xp`, `skill25rank`, `skill25lvl`, `skill25xp`) 
VALUES(1,1,now(), array_values_here)") or die(mysql_error());

谢谢你,najmeddine。这完美无瑕。 :)

4 个答案:

答案 0 :(得分:2)

    $query = sprintf("INSERT INTO `gotskills_clantrack`.`track_users` (`id`, `user_id`, `clan_id`, `date`, `skill1rank`, `skill1lvl`, `skill1xp`, `skill2rank`, `skill2lvl`, `skill2xp`, `skill3rank`, `skill3lvl`, `skill3xp`, `skill4rank`, `skill4lvl`, `skill4xp`, `skill5rank`, `skill5lvl`, `skill5xp`, `skill6rank`, `skill6lvl`, `skill6xp`, `skill7rank`, `skill7lvl`, `skill7xp`, `skill8rank`, `skill8lvl`, `skill8xp`, `skill9rank`, `skill9lvl`, `skill9xp`, `skill10rank`, `skill10lvl`, `skill10xp`, `skill11rank`, `skill11lvl`, `skill11xp`, `skill12rank`, `skill12lvl`, `skill12xp`, `skill13rank`, `skill13lvl`, `skill13xp`, `skill14rank`, `skill14lvl`, `skill14xp`, `skill15rank`, `skill15lvl`, `skill15xp`, `skill16rank`, `skill16lvl`, `skill16xp`, `skill17rank`, `skill17lvl`, `skill17xp`, `skill18rank`, `skill18lvl`, `skill18xp`, `skill19rank`, `skill19lvl`, `skill19xp`, `skill20rank`, `skill20lvl`, `skill20xp`, `skill21rank`, `skill21lvl`, `skill21xp`, `skill22rank`, `skill22lvl`, `skill22xp`, `skill23rank`, `skill23lvl`, `skill23xp`, `skill24rank`, `skill24lvl`, `skill24xp`, `skill25rank`, `skill25lvl`, `skill25xp`) 
    VALUES(1,1,now(), %s)", implode(", ", $array_values_here))

    $result = mysql_query($query);

答案 1 :(得分:1)

如果你想绕过输入$ array [1],$ array [2] ...你可以用一个FOR来建立你的查询字符串

如果你的查询是大的,mysql将无法运行它:
您只能插入10个字段,然后返回插入行的插入ID 然后用接下来的10个字段更新该行,直到你完成为止 不要忘记让所有列都接受空值

答案 2 :(得分:0)

如果要插入所有列(不跳过任何列),则可以省略列列表。但是,这不是一种好的做法,因为它使调试更加困难。

答案 3 :(得分:0)

你可以这样做:表格中的几个字段包含带分隔符的字符串(每个字段中有某种CSV)。

您可以充分利用您想要保存的所有内容。

例如:

Table.field1 := 'V01:1V02:2376V031804229942'
Table.field2 := 'V45:10V46:175'
...

问候。