PHP文件,如果条件得到不同的结果和错误

时间:2017-12-08 10:53:03

标签: php sql json sql-server

我有三个查询同时执行,并且它声明了一个对象$ sql。 结果"产品"阵列实际上四个记录只显示一个记录,三个记录不显示。在"总计"数组百分比值显示null。

我需要此结果

  {"success":1,"product":[{"std_Name":"VIVEK SANAPARA","Standard":"12-SCI-CE","Division":"A","ExamDate":{"date":"2016-10-06 00:00:00.000000","timezone_type":3,"timezone":"UTC"},"subject":"MATHS","ExamName":"WT","Marks":"30.00","TotalMarks":"30.00","PassingMarks":"10"},{"std_Name":"VIVEK SANAPARA","Standard":"12-SCI-CE","Division":"A","ExamDate":{"date":"2016-10-07 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Kolkata"},"subject":"PHYSICS","ExamName":"WT","Marks":"15.00","TotalMarks":"30.00","PassingMarks":"10"},{"std_Name":"VIVEK SANAPARA","Standard":"12-SCI-CE","Division":"A","ExamDate":{"date":"2016-10-08 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Kolkata"},"subject":"PHYSICS","ExamName":"WT","Marks":"25.00","TotalMarks":"30.00","PassingMarks":"10"},{"std_Name":"VIVEK SANAPARA","Standard":"12-SCI-CE","Division":"A","ExamDate":{"date":"2016-11-22 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Kolkata"},"subject":"PHYSICS","ExamName":"WT","Marks":"25.00","TotalMarks":"30.00","PassingMarks":"10"},],"total":[{"Marks":"30.00","TotalMarks":"30.00","Percentage":"79.166600"}],"exam":[{"ExamName":"WT"}]}

我在代码下面显示了错误图像。在图像中只有一个记录显示,但在上面的结果中,四个记录和百分比值在图像中显示为空。

Marks.php

if(isset($_REQUEST["insert"])) 
{ 
$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);

            $product = array();
            $product["std_Name"] = $stmt["std_Name"];
            $product["Standard"] = $stmt["Standard"];
            $product["Division"] = $stmt["Division"];
            $product["ExamDate"] = $stmt["ExamDate"];
            $product["subject"] = $stmt["subject"];
            $product["ExamName"] = $stmt["ExamName"];
            $product["Marks"] = $stmt["Marks"];
            $product["TotalMarks"] = $stmt["TotalMarks"];
            $product["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["product"] = array();
            $result["total"] = array();
            $result["exam"] = array();


            array_push($result["product"],$product);
            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  

    }


}

这是错误:

enter image description here

2 个答案:

答案 0 :(得分:0)

将括号中的百分比计算部分放在下面并尝试

(sum(a.Marks)/sum(a.TotalMarks) * 100) as Percentage

答案 1 :(得分:0)

在你的SQL查询中,没有名称为" Percentage"这就是你收到此错误的原因

$ sql ="选择b.std_Name,d.Standard,e.Division,a.ExamDate,f.subject,a.ExamName,a.Marks,a.TotalMarks,a.PassingMarks,缺少百分比"