Jump to content

Recommended Posts

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 "

 

Link to comment
https://forums.phpfreaks.com/topic/292169-how-do-i-get-this-value-of/
Share on other sites

Those values are "protected". That means you cannot access them in that way (http://php.net/manual/en/language.oop5.visibility.php).

 

 

Members declared protected can be accessed only within the class itself and by inherited and parent classes

 

 

I don't know why you would be trying to do that anyway. You have an object. You need to understand the methods that the object provides. You're apparently using mysqli, so why not just use the mysqli_ method to get the number of rows?

 

http://us1.php.net/manual/en/mysqli-result.num-rows.php

Edited by Psycho

i don't have access to the object creation itself.
I only have the above array

I would like to use any method possible to get that value.

echo mysqli_num_rows($mysqli_native_moodle_recordset);

gave me:

 

 

 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, object given

Edited by toolip

i don't have access to the object creation itself.

 

Not according to PHP

 

 

 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result,

object given 

 

Where, exactly, are you getting this array/object from?

Edited by Psycho

 

Not according to PHP

 

 

object given 

 

Where, exactly, are you getting this array/object from?

I don't understand what you mean.

the creation of the object is in a core php file in moodle that I am not allowed to edit.

something called dblib.php or something like that.

i'm working in a file called index.php.

 

that array above is the only thing I have.

the object is in there, from reason when I do this:

echo mysqli_num_rows($mysqli_native_moodle_recordset->result->mysqli_result);


it gives me this:

 

 

Fatal error: Cannot access protected property mysqli_native_moodle_recordset::$result

 

I cannot edit the creation of the object in dblib.php

Edited by toolip

You first need to use correct terminology. You keep switching back and forth between "object" and "array". Yu are dealing with an object. I know this because the first bit you displayed looks to be from a print_r

 

 

mysqli_native_moodle_recordset Object

        (

 

Then when you tried to use it, incorrectly, in num_rows(), it also told you it was an object

 

 

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, object given

 

Objects have properties and methods. You can see all the properties of the object in the print_r() that you did. Protected properties of a method cannot be accessed directly. They are built this way to prevent manipulation of data that would otherwise create invalid scenarios. Your object apparently represents a DB query result. If you were able to change 'num_rows' then it would not match the results. If there are protected or private  properties that are menat to be accessed there should be methods for the object to return that data. I don't know squat about moodle. But, you should be able to find by checking the documentation for it. But, there's a good chance they simply used the same name to retrieve that value. Try:

$mysqli_native_moodle_recordset->numrows();

Or

]$mysqli_native_moodle_recordset->num_rows();

If neither works, go check the documentation or post in a forum to get help with 3rd party frameworks.

Edited by Psycho
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.