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 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?
  • ¥50 cocos2d-x lua 在mac上接入lua protobuf?