如何三次查询执行一次

时间:2017-10-14 10:58:11

标签: php mysql sql

在PhP文件中三个查询如何执行。三个查询在sql_query中传递的不同三个变量,但它的执行时间在Image中给出错误。

错误图片

Marks.php

$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";


$sql1 = "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'";      

$sql2 = "select distinct ExamName From Marks_mas";                             ;
//$sql = "select * from Std_Reg";

$stmt = sqlsrv_query($conn, $sql,$sql1,$sql2);
$result = [];
do {
    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
       $result[] = $row; 
    }
} while (sqlsrv_next_result($stmt));

if(count($result)>0)
{
    $result1['status']=1;//"Login successfully";
    array_push($result,$result1);
}
else
{
     //$result[]="null";
    $result1['status']=0;//"Record not found";
    array_push($result,$result1);
}
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn); //Close the connnectiokn first

echo json_encode($result); //You will get the encoded array variable

2 个答案:

答案 0 :(得分:1)

你可以将多个SQL查询发送到一个SQL语句中,在第一个查询结束时用分号分隔!!

例如:

mysql_query("
    INSERT INTO table1 (field1,field2) VALUES(1,2);

    INSERT INTO table2 (field3,field4,field5) VALUES(3,4,5);

    DELETE FROM table3 WHERE field6 = 6;

    UPDATE table4 SET field7 = 7 WHERE field8 = 8; ");

在你的情况下它是这样的:

$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";


//$sql = "select * from Std_Reg";

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

供参考选中此项:http://php.net/manual/en/mysqli.quickstart.multiple-statement.php

答案 1 :(得分:-1)

在您的两个查询中将$reg替换为$_GET['reg']