PHP没有返回最新更新的sql过程结果

标签 php sql codeigniter mariadb mariasql

我正在使用 mariaDB、codeigniter php

在工作台中执行该过程时,它显示正确的结果。 bewlow image

但是当我使用 php codeigniter 运行相同的过程时,它返回不同的结果集。

array(1) {
[0]=>
array(1) {
    [0]=>
    array(2) {
        ["stuScore"]=> string(7) "44.0000"
        ["answerdQues"]=> string(2) "50"
    }
}
}

程序中的查询...

SELECT sum(Score) as stuScore, count(distinct ta1.idTestQuestion) as answerdQues
            FROM (select ta0.*, @running_time := if(@running_student = idStudent, @running_time, 0) + ta0.TimeTaken, date_add(ta0.StartTime, INTERVAL @running_time SECOND) as running_time, @running_student := idStudent
                from (select tap.idStudent, ta.score, ta.idTestQuestion, tap.StartTime, ta.TimeTaken
                    from `testanswerpaper` tap
                    left join testanswer ta on ta.idTestAnswerPaper = tap.idTestAnswerPaper and (ta.Status = 'Flagged' || ta.Status = 'Answered')
                    where  tap.`idTestQuestionPaper` = TestQuestionPaperID
                    order by tap.idStudent, ta.SortOrder, ta.idTestAnswer
                ) ta0
                join (select @running_time := 0, @running_student) running
            ) ta1
            join student s on s.idStudent = ta1.idStudent
            join user u on s.idUser = u.idUser
            WHERE ta1.running_time <= now()
            group by ta1.idStudent
            order by stuScore desc, answerdQues DESC;

php代码是

$this->readDB = $this->load->database('read', TRUE);
        $connectId = $this->readDB->conn_id ;
        $sql = "call GetLeaderBoardData($TestQuestionPaperID);";
        if (mysqli_multi_query($connectId,$sql))
        {
            do
            {
                // Store first result set
                if ($result=mysqli_store_result($connectId)) {

                        $resultArray[] = mysqli_fetch_all($result, MYSQLI_ASSOC);

                }
            } while (mysqli_next_result($connectId));

        } 
        var_dump($resultArray);

最佳答案

差异可能来自这样一个事实:当您从工作台与 codeigniter 执行代码时,用户定义的变量可能具有不同的值,因为用户定义的变量在整个 session 中保持其值。

要排除这种情况,请在过程开始时重置 @running_time@running_student 值。

set @running_time = null;
set @running_student = null;

SELECT sum(Score)...

关于PHP没有返回最新更新的sql过程结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58168082/

相关文章:

具有属性和内连接的 MySQL 查询

sql - PostgreSQL array_agg 但有停止条件

php - Codeigniter 日历不显示正确的数据并且设置限制不起作用

php - 在特定时间间隔向客户发送电子邮件

php - MySql 算子复杂度

php - mysql中列的排序和排名值

mysql - 在mysql中显示绝对计数器

php - 阻止对某些 URI 的 GET 访问

php - 无法在 mpdf 中使用 css-borders

php - 调用非对象的成员函数 InnerJoin()