【问题标题】:Php not returning the latest updated sql procedure resultphp没有返回最新更新的sql过程结果
【发布时间】:2020-01-29 18:13:46
【问题描述】:

我正在使用 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);

【问题讨论】:

  • 谁能帮帮我
  • 你为什么在 CodeIgniter 中使用 mysql 原始查询。您是否尝试过使用 $this->db->query( $sql ) ?
  • 你确定TestQuestionPaperID在PHP查询和数据库调用过程中是一样的吗?
  • 你想用这个存储过程做什么。最有可能的是,变量没有被执行(根据您对执行顺序的理解)。无论如何,这里给出了一些解释:stackoverflow.com/a/53465139 如果你需要帮助,你需要设置一个 Fiddle 和一些示例数据来玩。请阅读:Why should I provide a Minimal Reproducible Example for a very simple SQL query?
  • 还需要完整的程序代码

标签: php sql codeigniter mariadb mariasql


【解决方案1】:

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

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

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

SELECT sum(Score)...

【讨论】:

  • @running_time 初始化为零; @running_student 不是。
  • 但我已将@running_time 初始化为零。
  • 正如@RickJames 所说,@running_student 未初始化。当您在代码中使用用户定义变量 (@var) 时,请记住它们在整个会话期间保持其值,并且在您调用其他例程时也可以更改。
  • @438sunil - 我认为join (select @running_time := 0, @running_student) running 不会改变运行时间。
  • 即使你正确地初始化了变量,也不能保证 MariaDB 在 select 子句中计算 date_add(..., INTERVAL @running_time SECOND)@running_student := 之前会遵守 order by tap.idStudent... 或计算 @running_time := 。跨度>
【解决方案2】:

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

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);

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-07-07
      • 1970-01-01
      • 2014-09-23
      • 2015-10-17
      • 2014-07-04
      • 1970-01-01
      • 2011-05-03
      • 2013-02-19
      • 2013-04-23
      相关资源
      最近更新 更多