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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?