get_records_sql在moodle中使用内连接只返回一个结果

时间:2016-05-05 07:58:02

标签: php mysql moodle

我使用以下代码获取MOODLE中的类别和相应的课程详情

global $DB;

$catlist = $DB->get_records_sql(
'SELECT ca.id,ca.name,ca.coursecount, c.id as course_id , c.category,
c.fullname, c.shortname , c.summary , c.format, c.startdate , c.timecreated 
FROM {course_categories} as ca inner join {course} as c on ca.id = c.category 
WHERE ca.parent > ? and ca.visible = ? and c.visible = ? ', array('0','1','1'));

echo "<pre>";print_r($catlist); echo "</pre>";  

当我执行此查询时,我得到的结果数组只有一个结果行,而在mysql数据库中执行相同的sql会直接返回很多行。

表course_categories有2个类别'account','business'使用visible = 1具有活动条件,并且还包含父类别。 表课程有4个课程2,每个课程2与“帐户”和“商业”类别相关

结果如下:

Array
(
    [1] => stdClass Object
        (
            [id] => 1
            [name] => Accounts
            [coursecount] => 2
            [course_id] => 4
            [category] => 1
            [fullname] => Finance
            [shortname] => Finance
            [summary] => 

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

            [format] => weeks
            [startdate] => 1461695400
            [timecreated] => 1461653620
        )

    [2] => stdClass Object
        (
            [id] => 2
            [name] => Business
            [coursecount] => 2
            [course_id] => 5
            [category] => 2
            [fullname] => Animal Health Honours (BSc(Hons))
            [shortname] => Animal Health Honours
            [summary] => 

Sl/NO, Course Name, Duration. HARDWARE & NETWORKING. 1, Advacnce Diploma in Computer Hardware Maintanance & Networking(ADCHMN), 12 Months.

            [format] => weeks
            [startdate] => 1461781800
            [timecreated] => 1461760598
        )

)

任何人都可以帮助解决这个问题。

1 个答案:

答案 0 :(得分:1)

调用Moodle中任何get_records *函数的结果将作为结果中的第一个字段索引的数组返回(例如,如果您获得一组用户记录,那么非常有用,那么就要跳转直接到一条记录,基于用户ID)。

当您的查询返回categoryid作为第一个字段时,每个类别只返回1个结果(如果您将debugging设置为开发人员,则会收到有关此问题的警告。)

要修复,要么使用不同的字段作为返回的第一个值(在这种情况下,c.id将是一个很好的候选者),或者使用其中一个get_recordset *函数并使用foreach循环结果。