如何在没有foreach循环的情况下访问此对象属性

时间:2016-06-07 13:26:41

标签: php moodle

如何在不执行每个循环的情况下访问值overall_score?

我正在尝试获得整体价值,我尝试了$links->overall_score,但我得到了:

  

注意:未定义的属性:mysqli_native_moodle_recordset :: $ overall_score in   /Users/richard/Sites/moodle/moodle/report/link_critic/link_detail.php   在第90行

所以我正在做

$links = $DB->get_recordset_sql($query_links);

这是我从查询中找回的对象

mysqli_native_moodle_recordset Object
(
[result:protected] => mysqli_result Object
    (
        [current_field] => 0
        [field_count] => 17
        [lengths] => Array
            (
                [0] => 4
                [1] => 4
                [2] => 2
                [3] => 1
                [4] => 10
                [5] => 24
                [6] => 18
                [7] => 1
                [8] => 0
                [9] => 29
                [10] => 5
                [11] => 4
                [12] => 1
                [13] => 1
                [14] => 0
                [15] => 10
                [16] => 1
            )

        [num_rows] => 21
        [type] => 0
    )

[current:protected] => Array
    (
        [id] => 17397
        [resource_id] => 2512
        [page_id] => 15
        [user_id] => 1
        [link_submitted] => 1462649904
        [link_title] => Cold Spring Harbour page
        [link_description] => A useful brain map
        [link_status] => 1
        [link_broken] => 
        [link_url] => http://www.g2conline.org/2022
        [link_id] => 1583
        [vote_score] => 1
        [vote_comment] => 
        [vote_timestamp] => 1464795526
        [overall_score] => 1
    )

)

$ link->当前()

的Var转储
object(stdClass)#4476 (15) { ["id"]=> string(5) "17397" ["resource_id"]=> string(4) "2512" ["page_id"]=> string(2) "15" ["user_id"]=> string(1) "1" ["link_submitted"]=> string(10) "1462649904" ["link_title"]=> string(24) "Cold Spring Harbour page" ["link_description"]=> string(18) "A useful brain map" ["link_status"]=> string(1) "1" ["link_broken"]=> NULL ["link_url"]=> string(29) "http://www.g2conline.org/2022" ["link_id"]=> string(4) "1583" ["vote_score"]=> string(1) "1" ["vote_comment"]=> NULL ["vote_timestamp"]=> string(10) "1464795526" ["overall_score"]=> string(1) "1" } 

1 个答案:

答案 0 :(得分:0)

Recordset返回多条记录。

https://docs.moodle.org/dev/Data_manipulation_API#Using_Recordsets

因此,假设overall_score是sql中的一列,您应该使用:

$links = $DB->get_recordset_sql($query_links);
foreach ($links as $link) {
    echo $link->overall_score;
}
$links->close(); // This bit is very important!!!

如果您只想要一条记录,请使用get_record_sql()

$link = $DB->get_record_sql($query_links);
echo $link->overall_score;

如果您是Moodle的新手,可以将其固定在浏览器中以供参考:

https://docs.moodle.org/dev/Data_manipulation_API