douwei1921 2019-02-11 13:28
浏览 107
已采纳

异常 - 在NULL上调用成员函数get_record()

I'm trying to use the moodle Data Manipulation API to get the grades from students to analyse. But when I use the function get_record() inside of another function that I've created I get the message of the tittle. I don't know why the fuction works in the main and don't work inside a function.Any idea? I'm new in php and moodle manipulation, so take easy on me.

<?php
function get_all_quiz ($courseid) {

    $quizesid = [];
    $quiz = $DB->get_record('moodle.quiz', array('id'=>$courseid));
    $quizesid = $quiz.id;

    return $quizesid;
}

global $DB;
define('CLI_SCRIPT', true);
require '../../var/www/moodle/config.php';

$coursetest = 3;
$studentgrades = [];

$quizes = get_all_quiz($coursetest);
?>
  • 写回答

1条回答 默认 最新

  • douan9541 2019-02-11 14:47
    关注

    There are a few things you need to fix in this function:

    • You need to add global $DB; inside the function
    • The quiz database table is called 'quiz', not 'moodle.quiz' (Moodle config handles connecting to the correct database)
    • PHP uses '->' operator to access object properties, not '.' (which is used for concatenation), so it is $quiz->id not, $quiz.id
    • If you are wanting to return all quizzes, you should call $DB->get_records(), not $DB->get_record() (which returns just 1 record and outputs debugging warnings if more than one is found).
    • If you want the quizzes for a particular course, then you should match the 'course' field in the quiz record, not the 'id' field (which is the ID of the quiz, not the course).

    So the function should probably look like this:

    function get_all_quiz($courseid) {
        global $DB;
        return $DB->get_records('quiz', array('course' => $courseid));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题