How do I get the value of this field in moodle?

Hi

I have this object:

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

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

            [current:protected] => Array
                (
                    [id] => 148722
                    [picture] => 0
                    [firstname] => dari
                    [lastname] => bar
                    [firstnamephonetic] => 
                    [lastnamephonetic] => 
                    [middlename] => 
                    [alternatename] => 
                    [imagealt] => 
                    [email] => 
                    [username] => 
                    [city] => 
                    [country] => 
                    [lang] => 
                    [timezone] => 
                    [maildisplay] => 
                    [hidden] => 1
                    [ctxid] => 455402
                    [ctxpath] => 
                    [ctxdepth] => 
                    [ctxlevel] => 
                    [ctxinstance] => 148722
                )

        )

How do I get the value of “num_rows” (which is ‘10’) ?
I tried this:
$mysqli_native_moodle_recordset->result->num_rows

but got an error message: "Fatal error: Cannot access protected property mysqli_native_moodle_recordset::$result "

As the result property is protected you can’t access it from outside the class or its children. You can only access properties which are public, unless the class exposes the property through getter functions (eg getResult()).

Although I’m not familiar with Moodle, I had a quick look at the docs and, as far as I can see, it does not expose the result property. It looks like you would have to call fetch_next() recursively to loop through the result rows, in order to count them. Which is a bit mad.

Another way to get it is to create a class which extends mysqli_native_moodle_recordset, and give it a getResult() method:

class mysqli_native_moodle_recordset_with_getter extends mysqli_native_moodle_recordset {
     public function getResult(){
          return $this->result;
     }
     
     public function countRows(){
          return $this->result->num_rows;
     }
}

Does this help:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.