2017-01-20 69 views
1
echo "<pre>"; print_r($jobbrnchesids); exit; 

<pre>Array 
(
    [0] => Array 
     (
      [id_branch] => 6 
     ) 

    [1] => Array 
     (
      [id_branch] => 1 
     ) 
    ) 

從上面的數組,我得到工作ID分支..現在我正在嘗試讓這些分支的學生。 我已經嘗試過這種方式,但它出錯了無法調試任何人都可以幫助我。foreach不工作,而試圖讓學生

$studentBranch = ''; 
    foreach ($jobbrnchesids as $k => $v){ 
     $stuBranch = $conn->query("SELECT student_pid FROM tbl_students 
        WHERE graduation_branch = ".$v." "); 
     $studentsWithBranches[] = $stuBranch->fetch_assoc(); 
    } 
echo "<pre>"; print_r($studentsWithBranches); exit; 
+2

$ V [ 'id_branch'] –

+1

使用參數化查詢中,則返回的值。 – chris85

+0

它工作正常謝謝你。它在數組中分裂,我可以將所有學生存儲在一個數組中..? –

回答

3

,而不是$v它必須$v['id_branch']

3

丟失數組​​索引 'id_branch'

嘗試:

$stuBranch = $conn->query("SELECT student_pid FROM tbl_students 
       WHERE graduation_branch = ".$v['id_branch']." "); 
2

這樣做減少你的開銷如下:

$branchIds = array_column($jobbrnchesids,"id_branch"); 

$result = $conn->query("SELECT student_pid FROM tbl_students 
      WHERE graduation_branch IN (".implode(",",$branchIds.")"); 
$studentsWithBranches = $result?$result->fetch_all(MYSQLI_ASSOC):[]; 

array_column從單個列中的輸入數組

2
$studentBranch = ''; 
foreach ($jobbrnchesids as $k => $v) { 
    $stuBranch = $conn->query("SELECT student_pid FROM tbl_students WHERE graduation_branch = '" . ['id_branch'] . "'"); 
    $studentsWithBranches[] = $stuBranch->fetch_assoc(); 
} 
echo "<pre>"; 
print_r($studentsWithBranches); 
exit;