4

Im using mariaDB, codeigniter php

when execute the procedure in workbench it showing correct result. bewlow image

But when i run the same procedure using the php codeigniter its return different result set.

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

query in procedure...

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 code is

$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);
438sunil
  • 188
  • 1
  • 2
  • 13
  • can any one help me – 438sunil Oct 01 '19 at 08:57
  • Why are you using mysql raw queries inside CodeIgniter. Have you tried using $this->db->query( $sql ) ? – ascsoftw Oct 03 '19 at 06:00
  • Are you sure that TestQuestionPaperID is same in both PHP query and database calling procedure? – Sunny Oct 03 '19 at 06:21
  • 1
    What are you trying to do with this Stored procedure. Most likely, variables are not getting executed (as per your understanding of order of execution). Anyways, a bit of explanation is given here: https://stackoverflow.com/a/53465139 Still if you need assistance you need to setup a Fiddle with some sample data to play with. Please read: [Why should I provide a Minimal Reproducible Example for a very simple SQL query?](https://meta.stackoverflow.com/q/333952/2469308) – Madhur Bhaiya Oct 03 '19 at 07:41
  • And full procedure code needed also – illia permiakov Oct 03 '19 at 15:19
  • If you're using MariaDB then why are you using trickeries involving user variables? – Salman A Oct 09 '19 at 13:38
  • what version of MariaDB? – Rick James Oct 11 '19 at 01:49

3 Answers3

6

The difference might come from the fact that the user-defined variables might have different values when you execute the code from workbench vs codeigniter as the user-defined variables keep their values throughout the session.

To rule this out, reset the @running_time and @running_student values at the beginning of the procedure.

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

SELECT sum(Score)...
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
slaakso
  • 8,331
  • 2
  • 16
  • 27
  • `@running_time` is initialized to zero; `@running_student` is not. – Rick James Oct 04 '19 at 05:22
  • But i have intialized @running_time to zero. – 438sunil Oct 04 '19 at 06:32
  • 2
    As @RickJames said, the `@running_student` is not initialized. When you use user-defined variables (@var) in your code, keep in mind that they maintain their values throughout the session and can also change when you call other routines. – slaakso Oct 04 '19 at 08:40
  • @438sunil - I don't think `join (select @running_time := 0, @running_student) running` changes running time. – Rick James Oct 05 '19 at 00:05
  • 1
    Even if you initialize the variables properly, there is no guarantee that MariaDB will honor the `order by tap.idStudent...` or calculate `@running_time := ` before calculating `date_add(..., INTERVAL @running_time SECOND)` or before `@running_student := ` in the select clause. – Salman A Oct 10 '19 at 12:15
0

try this code or use $this->readDB->reconnect(); function just before procedure coll every time.

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);
Swarna Sekhar Dhar
  • 550
  • 1
  • 8
  • 25
0

Try var_dump on mysqli_fetch_all() statement that you have. It would give you idea of what’s being returned.

In your stored procedure, assign the complete query to a variable and print that to ensure that the sql is what you are expecting.

Shaunak Sontakke
  • 980
  • 1
  • 7
  • 17