所以这是我的 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_studentchoice
和 tbl_marks
。
我在 Project1
列上创建了一个值为 1 的学生数组。这就是我的 tbl_studentchoice 的样子:
现在我有了一组学生 ID。我想从另一个表 (tbl_marks
) 中获取这些学生的分数,如下所示:
并创建一个只有 studentid
和 Project1
值为 1 的新数组及其标记。所以基本上我想创建一个这样的数组:
Array (
[0] => Array (
[studentid] => c12558111
[marks] => 50
)
[1] => Array (
[studentid] => c12558112
[marks] => 60
)
)
最佳答案
为什么要使用两个不同的查询?? 你应该像这样连接表只写一个查询
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']);
}
关于php - 如何使用数组数据查询以在 PHP 中获取另一个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36969213/