I am creating a Moodle form using the [Forms API](https://docs.moodle.org/dev/lib/formslib.php_Form_Definition#select). I tried to populate a dropdown element in my form using a query.

```
class my_form extends moodleform {

    function definition() {
        $mform =& $this->_form;

        $mform->addElement('html', '<h2>' . get_string('header', 'local_data')  . '</h2>');

        global $USER;
        $userid = $USER->id;
        $myCourseIds = getMyCourses($userid);

        $mform->addElement('select', 'courseid', get_string('courseid', 'local_data'), $myCourseIds);

        $this->add_action_buttons(true, get_string('send', 'local_data'));
    }

}

```

Here's the query:

```
function getMyCourses($userid) {
    global $DB;

    $sql = 'SELECT c.idnumber, c.shortname
            FROM {role_assignments} ra, {user} u, {course} c, {context} cxt, {attendance_sessions} ass, {attendance} att
            WHERE ra.userid = u.id
            AND ra.contextid = cxt.id
            and att.id = ass.attendanceid
            and c.id = att.course
            AND cxt.instanceid = c.id
            AND (roleid =12 OR roleid=3)
            and u.id = ?';

    return $DB->get_records_sql($sql, array($userid));
}

```

The error that is returned is a general database error. I notice that the example in the [Moodle Forms API](https://docs.moodle.org/dev/lib/formslib.php_Form_Definition#select) page uses a global variable, $FORUM\_TYPES, where I have used the variable for my query. 

Here's the error message:

> 
> Coding error detected, it must be fixed by a programmer: PHP catchable fatal error. 
>  [More information about this error](http://docs.moodle.org/32/en/error/debug/codingerror)
> 
> 
> 

So my question is - 

* can I use a variable which stores the results of a database query in the select element of the Moodle forms API?