2016-05-01 39 views
0

另一个数据因此,这里是我的PHP代码:如何使用数组数据到查询来获取在PHP

$checkProject1Column = "SELECT * FROM tbl_studentchoice WHERE Project1 = '1'"; 
$resultProject1Column = mysqli_query($conn, $checkProject1Column);  
    while($row = mysqli_fetch_assoc($resultProject1Column)) { 
      //Create an array of students 
      $array[] = ['studentid' => $row['studentid']]; 
    } 
    //check marks database 
    $checkmarks = "SELECT studentid,marks FROM tbl_marks"; 
    $checkResult = mysqli_query($conn, $checkmarks); 
    if ($checkResult->num_rows >= 0){ 
     while($row1 = mysqli_fetch_assoc($checkResult)){ 
      //Create an array of students 
      $array1[] = [ 
       'marks' => $row1['marks'], 
       'studentid' => $row1['studentid'] 
       ]; 
      } 
     print_r($array); 
     print_r($array1); 

所以我有2个数据库表。 tbl_studentchoicetbl_marks。 我在Project1列创建了一个值为1的学生数组。这是我的tbl_studentchoice的样子:

projtbl

所以现在我有一个studentid的数组。我想从另一个表(tbl_marks),它看起来像这样让这些学生的痕迹:

marks

,并创建一个只studentid一个新的阵列Project1值1和他们的商标。所以基本上我想创建一个这样的数组:

Array ( 
    [0] => Array ( 
     [studentid] => c12558111 
     [marks] => 50 
    ) 
    [1] => Array ( 
     [studentid] => c12558112 
     [marks] => 60 
    ) 
) 
+0

是否有重复的'$ resultProject1Column = mysqli_query($康恩,$ checkProject1Column)的理由; '? – Amous

+0

其实不让我编辑我的代码 –

回答

4

为什么你使用两个不同的查询? 你应该加入表只写一个这样的查询

select s.*,m.marks 
    from tbl_studentchoice s, tbl_marks m 
    where s.studentid = m.studentid 
    and Project1 = '1' 

完整代码

$query= "select s.*,m.marks 
    from tbl_studentchoice s, tbl_marks m 
    where s.studentid = m.studentid 
    and Project1 = '1'"; 
$result= mysqli_query($conn, $query);  
while($row = mysqli_fetch_assoc($result)) { 

    echo("<br/>Student ID = ".$row['studentid']); 
    echo("<br/>Student marks = ".$row['marks']); 
} 
+0

非常感谢。我仍然没有足够的PHP经验,所以我不知道你可以做这样的查询。你能向我解释这个问题吗?我不完全理解它。我已经完成并创建了该阵列及其优点。我只是想多了解一下这个查询。 –

+0

它就像是一个内部连接,它将两个表连接在一起,在你的问题中有两个表有一个共同的列,所以这个查询会合并两个表的学生的数据,他们的学习结果是相同的 –

相关问题