Jump to content

Mysql query vs Php function not returning same values


huck
Go to solution Solved by huck,

Recommended Posts

Moodle 2.5

 

*nix server

 

Theme: Essential

 

----------------------

 

Hi Folks

 

 

I have a small mind bender in how php is returning results from a mysql query. There are two issues:

 

1) The mysql query from phpmyadmin is correct, while the php function that handles the query from the website is not.

2) It takes a very long time to run this query with php, 30 seconds to over a minute. Phpmyadmin is rather quick (Query took 0.0239 seconds).

 

The query is:

 


SELECT u.firstname AS 'Name' , u.lastname AS 'Surname', c.shortname AS 'Course', (
            CASE
            WHEN gi.itemname LIKE '%summative%' THEN 'SUMMATIVE'
            WHEN gi.itemname LIKE '%formative 2%' THEN 'FORMATIVE 2'
            ELSE 'MC'
            END) AS 'Assessment', from_unixtime(gi.timemodified, '%d/%m/%y') AS 'Date',
            IF (ROUND(gg.finalgrade / gg.rawgrademax * 100 ,2) > 70,'Yes' , 'No') AS Pass, ROUND(gg.finalgrade / gg.rawgrademax * 100 ,2) AS 'Mark', ROUND(gg.finalgrade / gg.rawgrademax * 100 ,2) AS 'Mark'
            FROM mdl_course AS c
            JOIN mdl_context AS ctx ON c.id = ctx.instanceid
            JOIN mdl_role_assignments AS ra ON ra.contextid = ctx.id
            JOIN mdl_user AS u ON u.id = ra.userid
            JOIN mdl_grade_grades AS gg ON gg.userid = u.id
            JOIN mdl_grade_items AS gi ON gi.id = gg.itemid
            JOIN mdl_course_categories AS cc ON cc.id = c.category
            WHERE gi.courseid = c.id
            AND gi.itemname != 'Attendance'
            AND u.firstname LIKE '%03%'
            AND gi.itemname LIKE '%mative%'
            ORDER BY  `Name` , `Surname` , `Course`, `Assessment` ASC

When I run the query in phpmyadmin , it gives back;

 

Name 		Surname		Category 	Module 		Course 		Assessment 	Date 		Competent 	Mark 	
G03 Itumeleng 	Velmah Mokwa 	Fundamentals 	Communications 	CO1 119472 	FORMATIVE 2 	07/04/14 	Yes 		100.00
G03 Itumeleng 	Velmah Mokwa 	Fundamentals 	Communications 	CO1 119472 	SUMMATIVE 	07/04/14 	Yes 		100.00
G03 Itumeleng 	Velmah Mokwa 	Fundamentals 	Communications 	CO2 119457 	FORMATIVE 2 	05/04/14 	Yes 		100.00
G03 Itumeleng 	Velmah Mokwa 	Fundamentals 	Communications 	CO2 119457 	SUMMATIVE 	05/04/14 	Yes 		88.00
G03 Lally 	Sheila Mokane 	Fundamentals 	Communications 	CO1 119472 	FORMATIVE 2 	07/04/14 	NYC 		59.00
G03 Lally 	Sheila Mokane 	Fundamentals 	Communications 	CO1 119472 	SUMMATIVE 	07/04/14 	Yes 		90.00
G03 Lally 	Sheila Mokane 	Fundamentals 	Communications 	CO2 119457 	FORMATIVE 2 	05/04/14 	Yes 		100.00
G03 Lally 	Sheila Mokane 	Fundamentals 	Communications 	CO2 119457 	SUMMATIVE 	05/04/14 	Yes 		98.00
And it is perfect so I have no issues with that. Now in php I call;

 


function print_overview_table_groups($COURSE, $choosegroup, $fromdate, $todate, $numarray)
{
    global $DB;
    //check data
    if(!$choosegroup){
        die('No Records To Display.');
    }
   
    $thisgroup = $numarray[$choosegroup];
       
        $sql = "SELECT DISTINCT u.firstname AS 'Name' , u.lastname AS 'Surname', 
			(CASE
			WHEN cc.parent = '2' THEN 'Fundamentals'
			WHEN cc.parent = '3' THEN 'Core'
			WHEN cc.parent = '4' THEN 'Elective'			
			END) AS 'Category',
			cc.name AS 'Module', 
			c.shortname AS 'Course', 
			(CASE
			WHEN gi.itemname LIKE '%summative%' THEN 'SUMMATIVE'
			WHEN gi.itemname LIKE '%formative 2%' THEN 'FORMATIVE 2'
			ELSE 'MC'
			END) AS 'Assessment', from_unixtime(gi.timemodified, '%d/%m/%y') AS 'Date',
			IF (ROUND(gg.finalgrade / gg.rawgrademax * 100 ,2) > 70,'Yes' , 'NYC') AS Competent, ROUND(gg.finalgrade / gg.rawgrademax * 100 ,2) AS 'Mark'
			FROM mdl_course AS c
			JOIN mdl_context AS ctx ON c.id = ctx.instanceid
			JOIN mdl_role_assignments AS ra ON ra.contextid = ctx.id
			JOIN mdl_user AS u ON u.id = ra.userid
			JOIN mdl_grade_grades AS gg ON gg.userid = u.id
			JOIN mdl_grade_items AS gi ON gi.id = gg.itemid
			JOIN mdl_course_categories AS cc ON cc.id = c.category
			WHERE gi.courseid = c.id
			AND gi.itemname != 'Attendance'
			AND u.firstname LIKE '%03%'
			AND gi.itemname LIKE '%mative%'
			ORDER BY  `Name` , `Surname` , `Course`, `Assessment` ASC";

        return $DB->get_records_sql($sql);
   
}

This is returned to the index.php page from the function call;

 


$lists = print_overview_table_groups($COURSE, $choosegroup, $fromdate, $todate, $numarray);
   
	print "<pre>";
	print_r($lists);
	print "</pre>";
The result is baffling...

 


Array
(
    [G03 Itumeleng] => stdClass Object
        (
            [name] => G03 Itumeleng
            [surname] => Mokwa
            [category] => Fundamentals
            [module] => Communications
            [course] => CO2 119457
            [assessment] => SUMMATIVE
            [date] => 05/04/14
            [pass] => Yes
            [mark] => 88.00
        )

    [G03 Lally] => stdClass Object
        (
            [name] => G03 Lally
            [surname] => Mokane
            [category] => Fundamentals
            [module] => Communications
            [course] => CO2 119457
            [assessment] => SUMMATIVE
            [date] => 05/04/14
            [pass] => Yes
            [mark] => 98.00
        )

)
I only get one record for each student.

 

Can anyone help me solve this?

 

 

Regards

 

Leon

Link to comment
Share on other sites

  • Solution

Hey Mac,

Thanks for the tip, it led me to the answer in a roundabout way. Moodle uses associative array on with the fucntion call get_records_sql(), and as such requires a proper 'id' field to build a fully populated query, if you use the wrong 'id', it filters out what it perceives as duplicates based on the 'id' value, so if the same 'id' has more than one record, the function will only return one record.

First I tried:

SELECT u.id , ...(rest of query)

and it failed, but then I tried:

SELECT gg.id , ...(rest of query)

It gave back the full listing as expected when I do a query through phpmyadmin.

Thanks guys, appreciate the help.

huck
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.