LMSouq
moodle-core Open

Moodle: Using SQL IN operator in $DB->get_records_sql

DA
David North
1 month ago
3 views
Problem Description
I can't seem to get the SQL IN operator to work with the correct moodle way of inputting variables into the sql string. When I do this: $activities = '('.$section->sequence.')'; $sql = "SELECT assign.name FROM {assign} assign INNER JOIN {course_modules} cm ON cm.instance = assign.id INNER JOIN {modules} m ON m.id = cm.module WHERE m.name = 'assign' AND cm.id IN ?"; $assignments = $DB->get_records_sql($sql, array($activities)); it doesn't work, it says there is an error in my sql syntax around the IN operator. But when I do it this way, it works. (Not using the ?) $sql = "SELECT assign.name FROM {assign} assign INNER JOIN {course_modules} cm ON cm.instance = assign.id INNER JOIN {modules} m ON m.id = cm.module WHERE m.name = 'assign' AND cm.id IN ".$activities; $assignments = $DB->get_records_sql($sql, null); How can I get it to work using the correct moodle syntax. ie. `$assignments = $DB->get_records_sql($sql, array($activities));`

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base