General developer forum

 
 
Andrew Normore
Re: Databases: How can I get enrolled users for a course using SQL
 

This code will return the user id's that are enrolled in course 2:


global $DB;

$enrolled = $DB->get_records_sql("

SELECT c.id, u.id

FROM {course} c
JOIN {context} ct ON c.id = ct.instanceid
JOIN {role_assignments} ra ON ra.contextid = ct.id
JOIN {user} u ON u.id = ra.userid
JOIN {role} r ON r.id = ra.roleid

where c.id = 2");

$count = count($enrolled);

echo "There are $enrolled users in course 2";

 

 

ENJOY smile

 
Average of ratings: -
Picture of Erwin Kroontje
Re: Databases: How can I get enrolled users for a course using SQL
 

I'm using above query, but how can i use this for determing if the userid exists in the results?

example: userid 2 is the result. then hide everything for him, but if user 4 logs in show it instead..

 

 
Average of ratings: -