2016-04-23 42 views
-2

我試圖通過循環訪問數組的值 - (稱爲列)來進行一系列mysqli查詢 - 但沒有任何內容正在返回。使用for循環制作多個mysqli查詢

for ($i = 0; $i < sizeof($column); $i++) 

{ 
    $mutualInterests = mysqli_query ($conn, "SELECT USER_1 FROM INTERESTS WHERE answer = " . $column[$i]); 

    while ($row = mysqli_fetch_array ($mutualInterests)) 

    { 
     echo " $row[USER_1]"; 

    } 


} 
+2

爲什麼不直接使用'in'? –

+0

這樣更有效地使用IN,就像這樣''SELECT USER_1 FROM INTERESTS WHERE IN INPUT IN(「。implode(',',$ column)。」)「' –

+0

顯示'$ column'的示例 – RiggsFolly

回答

-1

只需用下面的代碼替換你給出的代碼,看看導致..........

$query='SELECT USER_1 FROM INTERESTS WHERE answer ="'.$column[0].'"'; 

for ($i = 1; $i < sizeof($column); $i++) 

{ 
$query.=' OR answer ="'.$column[$i].'"'; 

} 

$mutualInterests = mysqli_query ($conn,$query); 

while ($row = mysqli_fetch_array ($mutualInterests)) 

{ 
    echo " $row[USER_1]"; 

} 
+0

現在工作,謝謝! – ianmc

+0

繼續走下去! – shafiul

-1

替換此:

while ($row = mysqli_fetch_array ($mutualInterests)) 

{ 
    echo " $row[USER_1]"; 

} 

與此:

$count = 0; 
while ($row = mysqli_fetch_array ($mutualInterests)) 
{ 
    echo $row[$count]."<br>"; 
    $count++; 

} 

讓我知道,如果現在的工作! :)

+0

這似乎並不適用於我:/ – ianmc

+0

@ianmc我修改了我的答案,現在嘗試 – Webeng

+0

仍然不起作用恐怕 – ianmc