toolip Posted October 30, 2014 Share Posted October 30, 2014 HiI 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 " Quote Link to comment https://forums.phpfreaks.com/topic/292169-how-do-i-get-this-value-of/ Share on other sites More sharing options...
Psycho Posted October 30, 2014 Share Posted October 30, 2014 (edited) 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 October 30, 2014 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/292169-how-do-i-get-this-value-of/#findComment-1495269 Share on other sites More sharing options...
toolip Posted October 30, 2014 Author Share Posted October 30, 2014 (edited) 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 October 30, 2014 by toolip Quote Link to comment https://forums.phpfreaks.com/topic/292169-how-do-i-get-this-value-of/#findComment-1495273 Share on other sites More sharing options...
Psycho Posted October 30, 2014 Share Posted October 30, 2014 (edited) 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 October 30, 2014 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/292169-how-do-i-get-this-value-of/#findComment-1495276 Share on other sites More sharing options...
toolip Posted October 30, 2014 Author Share Posted October 30, 2014 (edited) 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 October 30, 2014 by toolip Quote Link to comment https://forums.phpfreaks.com/topic/292169-how-do-i-get-this-value-of/#findComment-1495277 Share on other sites More sharing options...
toolip Posted October 30, 2014 Author Share Posted October 30, 2014 I edited the last reponse , please take a look Quote Link to comment https://forums.phpfreaks.com/topic/292169-how-do-i-get-this-value-of/#findComment-1495280 Share on other sites More sharing options...
Psycho Posted October 30, 2014 Share Posted October 30, 2014 (edited) 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 October 30, 2014 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/292169-how-do-i-get-this-value-of/#findComment-1495283 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.