【问题标题】:Get specific field from a table with moodle Data manipulation API使用 moodle 数据操作 API 从表中获取特定字段
【发布时间】:2016-10-14 20:02:26
【问题描述】:

我想从moodle数据库中选择id等于某个变量的所有记录。谁能告诉我moodle数据操作API的语法是什么。

这里是mysql查询

Select question from mdl_answers where id=$questionid;

我试过了

$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

但它只提供一条记录,而我有多个记录与此 ID 相关联。

正如您在图片记录中看到的那样,71 和 72 在下一列中具有相同的值 48,我想获得这些 id 71 和 72

【问题讨论】:

  • 你不应该直接将参数替换为 SQL 查询,这会导致 SQL 注入。
  • “我得到这个”是什么意思
  • 我只得到价值 71

标签: mysql moodle moodle-api


【解决方案1】:

get_field() 只会返回一个值。

要么使用get_records() - https://docs.moodle.org/dev/Data_manipulation_API#Getting_an_hashed_array_of_records

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

或者像这样使用get_field()

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

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多