如何在查询中使用数组数据获取PHP中的其他数据

时间:2022-05-28 03:36:27

So here is my PHP code:

这是我的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);

So I have 2 database tables. tbl_studentchoice and tbl_marks. I created an array of students with a value 1 on Project1 column. This is how my tbl_studentchoice looks like:

我有两个数据库表。tbl_studentchoice tbl_marks。我在Project1列中创建了一个值为1的学生数组。我的tbl_studentchoice是这样的:

如何在查询中使用数组数据获取PHP中的其他数据.

So now that I have an array of studentid's. I want to get the marks of these students from another table (tbl_marks) which looks like this:

现在我有了一组studentid。我想从另一个表(tbl_marks)中获取这些学生的分数,它看起来是这样的:

如何在查询中使用数组数据获取PHP中的其他数据

and create a new array with only studentid with Project1 value 1 and their marks. So basically I want to create an array like this:

并创建一个仅带有Project1值1及其标记的新数组。基本上我想创建一个这样的数组:

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

1 个解决方案

#1


4  

Why are you using two diffrent query?? you should join tables only write one query like this

为什么要使用两个不同的查询?您应该只编写一个这样的查询来连接表

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

Complete Code

完整的代码

$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']);
}

#1


4  

Why are you using two diffrent query?? you should join tables only write one query like this

为什么要使用两个不同的查询?您应该只编写一个这样的查询来连接表

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

Complete Code

完整的代码

$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']);
}