4

我正在使用 mariaDB,codeigniter php

在工作台中执行程序时,它显示正确的结果。 下图

但是当我使用 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);
4

3 回答 3

6

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

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

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

SELECT sum(Score)...
于 2019-10-03T20:35:07.467 回答
0

$this->readDB->reconnect();每次都在过程 coll 之前尝试此代码或使用函数。

try {
            $this->readDB = $this->load->database('read', TRUE);
            $this->readDB->reconnect();
            $sql = "CALL GetLeaderBoardData(".$TestQuestionPaperID.")";
            $resultArray = $this->readDB->query($sql)->custom_result_object(); 
            $this->readDB->close();
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        var_dump($resultArray);
于 2019-10-04T12:17:12.963 回答
0

在您拥有的 mysqli_fetch_all() 语句上尝试 var_dump。它会让您了解返回的内容。

在您的存储过程中,将完整的查询分配给一个变量并打印它以确保 sql 是您所期望的。

于 2019-10-09T14:13:09.393 回答