PHP - while循环问题

时间:2017-12-22 09:40:45

标签: php arrays json sql-server

我从Ms SQL Server 2008获取数据。我一次执行三个查询执行和一个语句。第一个查询是学生详细信息和标记,第二个查询是总分和百分比,第三个查询是考试名称。

我有问题是虽然循环只显示一条记录但实际上有四条记录。三个记录不显示。百分比值显示为空。

Marks1.php

export NLS_LANG=[NLS_LANGUAGE]_[NLS_TERRITORY].[NLS_CHARACTERSET]
For example: American_America.UTF8

在图片中给出错误:

enter image description here

在图像标记中,数组记录显示一个但实际上有四个记录。三个记录不显示。总数组百分比值为null。

下面给出实际的Result.In标记数组显示四个记录和总数组给出百分比值。

我需要这个结果:

$reg = $_GET['reg'];

$sql = "select b.std_Name,d.Standard,e.Division,a.ExamDate,f.subject,a.ExamName,a.Marks,a.TotalMarks,a.PassingMarks 
                                  from Marks_mas a  inner join std_reg b on a.regno=b.regno 
                                  INNER JOIN Subject_mas as f ON a.Subject_ID = f.Subject_ID 
                                  inner join StandardMaster d on a.standard = d.STDID 
                                  inner join DivisionMaster e on a.Division = e.DivisionID 
                                  where a.RegNo= '$reg' order by a.ExamDate; select   sum(a.Marks) as Marks,sum(a.TotalMarks) as TotalMarks, (sum(a.Marks)/sum(a.TotalMarks) * 100) as Percentage 
                                   from Marks_mas a 
                                   where a.RegNo= '$reg'; select distinct ExamName From Marks_mas;";

$stmt = sqlsrv_query($conn, $sql);

$result = array(); 

 if (!empty($stmt)) {
        // check for empty result
        if (sqlsrv_has_rows($stmt) > 0) {

            //$stmt = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);



             while ($stmt = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
            $marks = array();
            $marks["std_Name"] = $stmt["std_Name"];
            $marks["Standard"] = $stmt["Standard"];
            $marks["Division"] = $stmt["Division"];
            $marks["ExamDate"] = $stmt["ExamDate"];
            $marks["subject"] = $stmt["subject"];
            $marks["ExamName"] = $stmt["ExamName"];
            $marks["Marks"] = $stmt["Marks"];
            $marks["TotalMarks"] = $stmt["TotalMarks"];
            $marks["PassingMarks"] = $stmt["PassingMarks"];


            $total = array();
            $total["Marks"] = $stmt["Marks"];
            $total["TotalMarks"] = $stmt["TotalMarks"];
            $total["Percentage"] = $stmt["Percentage"];

            $exam = array();
            $exam["ExamName"] = $stmt["ExamName"];

            }




             // success
            $result["success"] = 1;


            // user node
            $result["marks"] = array();
            $result["total"] = array();
            $result["exam"] = array();


            array_push($result["marks"],$marks);
            array_push($result["total"],$total);
            array_push($result["exam"],$exam);

            // echoing JSON response
            echo json_encode($result);


        } else {
            // no product found
            $result["success"] = 0;
            $result["message"] = "No product found";

            // echo no users JSON
            echo json_encode($result);

        }

        //sqlsrv_free_stmt($stmt);
        sqlsrv_close($conn); //Close the connnection first  

    }

1 个答案:

答案 0 :(得分:0)

使用左连接我假设Marks_mas是连接到其他表的主表。

$sql = "select b.std_Name,d.Standard,e.Division,a.ExamDate,f.subject,a.ExamName,a.Marks,a.TotalMarks,a.PassingMarks 
                              from Marks_mas a  LEFT join std_reg b on a.regno=b.regno 
                              LEFT JOIN Subject_mas as f ON a.Subject_ID = f.Subject_ID 
                              LEFT join StandardMaster d on a.standard = d.STDID 
                              LEFT join DivisionMaster e on a.Division = e.DivisionID 
                              where a.RegNo= '$reg' order by a.ExamDate; select   sum(a.Marks) as Marks,sum(a.TotalMarks) as TotalMarks, (sum(a.Marks)/sum(a.TotalMarks) * 100) as Percentage 
                               from Marks_mas a 
                               where a.RegNo= '$reg'; select distinct ExamName From Marks_mas;";