General developer forum

get_records_sql returns one user/course/activity/etc when there are many

get_records_sql returns one user/course/activity/etc when there are many

I'm having a strange problem. I have been a PHP/Moodle developer for a while now. But this is unique, to me. Thought I would post here to see if you could help me out.

I have a  PHP script that taps into the Moodle code. I've done this many times. So no problems there. The problem is that database queries are coming back from the database truncated. For example, I use this bit of code:

If I query it directly in MySQL it returns all the students who are students in the given course. In one particular course I was using for testing there were about 25 students.

SELECT c.id AS id, c.fullname, u.id as uid, u.username, u.firstname, u.lastname, u.email
FROM mdl_role_assignments ra, md_user u, mdl_course c, mdl_context cxt
WHERE ra.userid = u.id
AND ra.contextid = cxt.id
AND cxt.contextlevel =50
AND cxt.instanceid = c.id
AND c.id =23251
AND roleid = (select id from mdl_role where shortname='student');

But when I have it in my program like the below it only has an array of one student--most always the last one in the database. So if I did a var_dump of $students below I would see only one of the 25 users that this query would return for one example test course.$students = $DB->get_records_sql("SELECT c.id AS id, c.fullname, u.id as uid, u.username, u.firstname, u.lastname, u.email FROM {role_assignments} ra, {user} u, {course} c, {context} cxt WHERE ra.userid = u.id AND ra.contextid = cxt.id AND cxt.contextlevel =50 AND cxt.instanceid = c.id AND c.id =? AND roleid = (select id from mdl_role where shortname='student')",array($courseID));

I have even formatted the queries as recordsets and also as non-parametized queries. Same behavior. I've cleared the caches. Same behavior. I've tried other courses. Same behavior.

It is not just this query. It is all queries where I query the database for users, courses, etc. So what am I missing here? Why are they not all coming back from a program with this SQL in it when they do when I query the database directly with just SQL?

Average of ratings: -
Re: get_records_sql returns one user/course/activity/etc when there are many

As stated in the docs https://docs.moodle.org/dev/Data_manipulation_API#Getting_an_hashed_array_of_records the array returned by all of the get_records* functions in Moodle is indexed by the first field in the SELECT.

So, if you set the first field to c.id, then you will only get a single record for each course, as the others will be overwritten.

If you have Debugging on (as you should always have for any development), then you will get a warning message about this.

Solutions:

1. Choose a different field at the start of the SELECT statement (ra.id would be a good choice in this case)

2. Use one of the get_recordset* functions and loop through the results with foreach (this also reduces memory usage, but has less flexibility as you can only loop through the results, you cannot use any of the usual array manipulation functions on them).

Average of ratings: -
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.24709492921829224, "perplexity": 2315.090977746993}, "config": {"markdown_headings": false, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2018-34/segments/1534221218357.92/warc/CC-MAIN-20180821151743-20180821171743-00707.warc.gz"}