PHP Mysqli选择提供的多行数据foreach

时间:2016-01-08 08:46:40

标签: php mysqli

我试图从每个ID的2个不同表格中获取三件事,即对我RegID

  1. 我的第一个查询是选择DISTINCT RegID from table_one如果有多个AttendanceStatus =Absent,则应选择AttendanceDate = Todays_dateRegID

  2. 选择的每个DISTINCT Subjects
  3. 应从同一Standard

  4. 中选择table_onetable_two
  5. 相同的ID应该从RegID选择手机号码,它应该回显然后重复第二个RegID如果存在同样应该重复,直到最后DISTINCT
  6. 我正在获取AttendanceStatus regid但是在同一个while语句中如果使用获取结果来获取主题和标准以及注册ID它只给我一个id,主题和标准并且我没有得到如何解决这个问题

    我的编辑 现在在我的示例数据库中,我有两个条目表示RegID中缺席,所以在我的第一个选择i vl删除重复项(如果存在)并选择那些 2 RegID正常工作对于每个Standard,我想在我的第二个查询中选择subjectRegID,而在while语句中,有两个RegID它只给我一个我需要那些两个缺席的标准与各自不同的主题和标准一起出现

    简单

    1. 首先Standard通过删除重复
    2. 来选择不在场的人
    3. 为首先选择的Subjects
    4. 选择RegID'sRegID
    5. 为每个Standard
    6. 单独回复SubjectsRegIDAttendanceDate Standard Subjects RegID AttendanceStatus 2016-01-08 00:00:00 III BSc PCM PHY/CHEM PRACTICAL 1382043 Present 2016-01-08 00:00:00 III BSc PCM PHY/CHEM PRACTICAL 1382044 Present 2016-01-08 00:00:00 III BSc PCM PHY/CHEM PRACTICAL 1382045 Present 2016-01-08 00:00:00 III BSc PCM PHY/CHEM PRACTICAL 1382046 Absent 2016-01-08 00:00:00 III BSc PCM PHY/CHEM PRACTICAL 1382047 Absent

      我的table_one数据库

      $query="SELECT DISTINCT RegID FROM table_one WHERE AttendanceDate='2016-01-08 00:00:00' and AttendanceStatus='Absent'" ;
      $data=mysqli_query($mysqli,$query)or die(mysqli_error());
      if(mysqli_num_rows($data) > 0) {
          while($row=mysqli_fetch_array($data)){
              $StudentRegID= $row['RegID'];
              $query="SELECT   DISTINCT(CONCAT(Standard,Subjects)) AS standard_and_subject , RegID FROM table_one WHERE AttendanceDate='2016-01-08 00:00:00' and RegID='$StudentRegID'" ;
              $data=mysqli_query($mysqli,$query)or die(mysqli_error());
              if(mysqli_num_rows($data) > 0) {
                  while($row=mysqli_fetch_array($data)){
                      if($row['RegID'] != '' && $row['RegID'] != NULL){
                          $RegID = $row['RegID'];
                          $standard_and_subject = $row['standard_and_subject'];
                          echo $standard_and_subject;
                          echo $RegID;
                      }
                  }
              }
          }
      }
      

      PHP Mysqli代码

      getAjax()

2 个答案:

答案 0 :(得分:2)

使用JOIN的单个查询,而不是foreach循环内的查询。而不是在PHP中检查空RegID,而是在SQL中过滤掉它们。

$query = "SELECT DISTINCT t1.RegID, CONCAT(t2.Standard, t2.Subjects) AS standard_and_subject
          FROM (SELECT DISTINCT RegID
                FROM table_one
                WHERE AttendanceDate='2016-01-08 00:00:00'
                AND AttendanceStatus='Absent'
                AND RegID != ''
                AND RegID IS NOT NULL) AS t1
          JOIN table_one AS t2 ON t1.RegID = t2.RegID
          WHERE t2.AttendanceData = '2016-01-08 00:00:00'";
$data = mysqli_query($query) or die(mysqli_error());
if (mysqli_num_rows($data) > 0) {
    while ($row = mysqli_fetch_assoc($data)) {
        echo $row['standard_and_subject'];
        echo $row['RegID'];
    }
}

答案 1 :(得分:0)

您的商店mysql对象导致相同的变量名称

尝试此编码

 while($standard_row=mysqli_fetch_array($data)){

                    if($standard_row['RegID'] != '' && $standard_row['RegID'] != NULL){
                        $RegID = $standard_row['RegID'];

                            $standard_and_subject = $standard_row['standard_and_subject'];
                        echo $standard_and_subject;
                        echo $RegID;

                    }