将mysqli查询结果读入多维数组

时间:2016-01-13 12:27:55

标签: php multidimensional-array mysqli

我有一个代码来根据用户输入序列从MySQL数据库中读取数据,我希望将所有结果存储到一个多维数组中,以便在以后的部分中使用。但是,代码只存储最后一个数组,但不存储每个数组。我的代码有什么问题吗?

for($i = 0 ; $i < sizeof($up_gene) ; $i ++){
    $geneName = $up_gene[$i];
    $mysql = "SELECT * FROM clmap WHERE GeneID = '$geneName'";
    $result = mysqli_query($con, $mysql);
    $row = mysqli_fetch_array($result , MYSQLI_NUM);
    $up_temp[$i] = $row;
}

我的数据库中的数据如下:

GeneID  | 17_AAG | HUTYG | A_770897 | AICAR | ...
-------------------------------------------------
FR2L1   | 1390   | 6746  | 4316     | 1051  | ...
ALDH1A1 | 288    | 7622  | 12       | 98867 | ...

# other rows is similar to above #

如果我输入GeneID列表,例如FR2L1ALDH1A1ABATAAAS, 输出数组只有最后一个数组的值,在这种情况下为$up_temp[3],其余部分从$up_temp[0]$up_temp[2]为空。数据库中有数百万行,但我只需要选择用户的输入GeneID

print_r($up_temp[3])

的输出
  

数组([0] =&gt; F2RL1 [1] =&gt; 1390 [2] =&gt; 6764 [3] =&gt; 4316 [4] =&gt; 1051   [5] =&gt; 11654 [6] =&gt; 1871 [7] =&gt; 8863 [8] =&gt; 1245 [9] =&gt; 6438 [10] =&gt;   12693 [11] =&gt; 7525 [12] =&gt; 10372 [13] =&gt; 10625 [14] =&gt; 7114 [15] =&gt;   12180 [16] =&gt; 2760 [17] =&gt; 375 [18] =&gt; 12525 [19] =&gt; 2828 [20] =&gt; 6938   [21] =&gt; 5926 [22] =&gt; 8906 [23] =&gt; 9372 [24] =&gt; 6341 [25] =&gt; 1427 [26]   =&GT; 3858 [27] =&gt; 1194 [28] =&gt; 3510 [29] =&gt; 2462 [30] =&gt; 3301 [31] =&gt; 1909年[32] =&gt; 3816 [33] =&gt; 8901 [34] =&gt; 5361 [35] =&gt; 2711 [36] =&gt; 5294   [37] =&gt; 6138 [38] =&gt; 10272 [39] =&gt; 9893 [40] =&gt; 1205 [41] =&gt; 10868   [42] =&gt; 3016 [43] =&gt; 1601 [44] =&gt; 315 [45] =&gt; 2577 [46] =&gt; 6226 [47]   =&GT; 274 [48] =&gt; 1573 [49] =&gt; 2681 [50] =&gt; 1447 [51] =&gt; 5375 [52] =&gt; 1381 [53] =&gt; 6405 [54] =&gt; 11033 [55] =&gt; 7915 [56] =&gt; 8293 [57] =&gt; 3446   [58] =&gt; 231 [59] =&gt; 9772 [60] =&gt; 4684 [61] =&gt; 432 [62] =&gt; 1815 [63] =&gt;   10904 [64] =&gt; 41 [65] =&gt; 4647 [66] =&gt; 3238 [67] =&gt; 5488 [68] =&gt; 2340   [69] =&gt; 4925 [70] =&gt; 2513 [71] =&gt; 911 [72] =&gt; 3736 [73] =&gt; 6593 [74]   =&GT; 1441 [75] =&gt; 3098 [76] =&gt; 2976 [77] =&gt; 4665 [78] =&gt; 8056 [79] =&gt; 10717 [80] =&gt; 53 [81] =&gt; 4159 [82] =&gt; 9920 [83] =&gt; 5673 [84] =&gt; 12146   [85] =&gt; 2072 [86] =&gt; 4753 [87] =&gt; 9678 [88] =&gt; 6811 [89] =&gt; 7519 [90]   =&GT; 10708 [91] =&gt; 1215 [92] =&gt; 6837 [93] =&gt; 6817 [94] =&gt; 6085 [95] =&gt; 7160 [96] =&gt; 9059 [97] =&gt; 7729 [98] =&gt; 5330 [99] =&gt; 179 [100] =&gt; 1020   [101] =&gt; 8672 [102] =&gt; 9971 [103] =&gt; 7189 [104] =&gt; 2307 [105] =&gt; 2849   [106] =&gt; 4897 [107] =&gt; 1229 [108] =&gt; 11588 [109] =&gt; 1923 [110] =&gt;   12715 [111] =&gt; 2099 [112] =&gt; 12035 [113] =&gt; 4062 [114] =&gt; 1416 [115]   =&GT; 547 [116] =&gt; 1915 [117] =&gt; 7689 [118] =&gt; 6641 [119] =&gt; 11312 [120] =&gt; 7079 [121] =&gt; 8564 [122] =&gt; 2156 [123] =&gt; 539 [124] =&gt; 4850 [125] =&gt; 10205 [126] =&gt; 1756 [127] =&gt; 1266 [128] =&gt; 3468 [129] =&gt; 7341 [130] =&gt; 8809 [131] =&gt; 11949 [132] =&gt; 10126 [133] =&gt; 3436 [134] =&gt; 8066 [135] =&gt; 3116 [136] =&gt; 4155 [137] =&gt; 1637 [138] =&gt; 1717 [139] =&gt; 2045   [140] =&gt; 7511)

print_r($up_temp[0])print_r($up_temp[2])的输出:空

更新:事实证明我的$up_gene变量未正确输入,只是意识到每个变量后面都有空间导致MySQL查询返回前几个变量为空。上面的代码按预期工作。

1 个答案:

答案 0 :(得分:2)

尝试这样做:

$up_temp = array();
$sql = "select * from clmap where GeneID in(".implode(",",$up_gene).");";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result , MYSQLI_NUM)){
    $up_temp[] = $row;
}

内爆数组$up_gene,以便所有ID都是逗号分隔的字符串。然后使用mysql in来获取具有其中一个id的所有行。至少运行一个while循环并将$ row数组存储到多维数组$ up_temp