php - 如何使用数组数据查询以在 PHP 中获取另一个数据

标签 php mysql arrays database

所以这是我的 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 .

现在我有了一组学生 ID。我想从另一个表 (tbl_marks) 中获取这些学生的分数,如下所示:

marks

并创建一个只有 studentidProject1 值为 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/

相关文章:

php - 如何在不收到错误响应的情况下与 ObjectiveC 中的服务器进行交互?

php - 想在 Zend 框架之外使用 Zend_translate 吗?

php - 如何访问xampp主页?

php - 有什么方法可以将值从 JavaScript 发送到 CodeIgniter 中的 Controller 吗?

mysql - 如何在 MySQL If 语句中有一个范围?

java - 将整数添加到 int 数组

选择值后MySql变量为空

php - 对日期时间字段运行选择查询时出错

php - 如何在 PHP 中使用 ArrayIterator append 带有键的元素?

c++ - ERROR 函数问题。 C++