Usman Qadeer Usman Qadeer - 13 days ago 7
MySQL Question

Get specific field from a table with moodle Data manipulation API

I want to select all records from moodle data base where id is equal to a certain variable.Can anybody tell me what will be the moodle Data manipulation API syntax for it.

Here is mysql query

Select question from mdl_answers where id=$questionid;

I have tried

$queid[]= $queid[] = $DB->get_field('question_answers', 'id', array('question' => $questionid), MUST_EXIST);
$true=$queid[0]; // this i get
$false=$queid[1]; //this remains empty

But it only gives one record whereas i have more than one records assosiated to this id.

as you can see in the picture record 71 and 72 have same value 48 in next column i want to get these ids 71 and 72
enter image description here


get_field() will only return a single value.

Either Use get_records() -

$answers = $DB->get_records('question_answers', array('question' => $questionid));
foreach ($answers as $answer) {
    switch ($answer->answer) {
        case 'True':
            $true = $answer->id;
        case 'False':
            $false = $answer->id;

Or use get_field() like this

$true = $DB->get_field('question_answers', 'id', array('question' => $questionid, 'answer' => 'True'));
$false = $DB->get_field('question_answers', 'id', array('question' => $questionid, 'answer' => 'False'));