programming.name Posted May 10, 2010 Share Posted May 10, 2010 Hi, Please consider the following snippet: $mysqli = new mysqli('host', 'user_name', 'password', db_name''); //line 1 $result = $mysqli->query($query); //line 2 $r = $result->num_rows; //line 3 I am pretty comfortable with the line 1 and 2; line 1 creates an object called '$mysqli' and because mysqli class as built-in class takes 4 arguments the line 1 is perfectly fine. And line 2, it accesses to the method, which is already defined in the mysqli class, called query which takes one argument using access operator(->). But I have no idea why the access operator(->) is used on line 3. I think the variable $result is not an object but just a variable in terms of OOP(because it didn't create any object unlike $mysqli). And I found that "$mysqli->query($query)->num_rows;" is also possible. Could you explain what is going on with the line 3 please?! Thanks Quote Link to comment Share on other sites More sharing options...
aeroswat Posted May 10, 2010 Share Posted May 10, 2010 Hi, Please consider the following snippet: $mysqli = new mysqli('host', 'user_name', 'password', db_name''); //line 1 $result = $mysqli->query($query); //line 2 $r = $result->num_rows; //line 3 I am pretty comfortable with the line 1 and 2; line 1 creates an object called '$mysqli' and because mysqli class as built-in class takes 4 arguments the line 1 is perfectly fine. And line 2, it accesses to the method, which is already defined in the mysqli class, called query which takes one argument using access operator(->). But I have no idea why the access operator(->) is used on line 3. I think the variable $result is not an object but just a variable in terms of OOP(because it didn't create any object unlike $mysqli). And I found that "$mysqli->query($query)->num_rows;" is also possible. Could you explain what is going on with the line 3 please?! Thanks Does $mysqli->query($query) not act as a constructor? If it doesn't then you are right. It's not truly OOP and they are using it the wrong way. Edit: When I say act as a constructor I mean does it call a constructor of its own and return a class object Quote Link to comment Share on other sites More sharing options...
aeroswat Posted May 10, 2010 Share Posted May 10, 2010 mysqli_result is indeed a class. Read up about it at php site http://php.net/manual/en/class.mysqli-result.php Quote Link to comment Share on other sites More sharing options...
ignace Posted May 10, 2010 Share Posted May 10, 2010 Does $mysqli->query($query) not act as a constructor? If it doesn't then you are right. It's not truly OOP and they are using it the wrong way. Edit: When I say act as a constructor I mean does it call a constructor of its own and return a class object The method query() returns a MySQLi_STMT object, num_rows is a property that contains the number of rows by the last executed query. Quote Link to comment Share on other sites More sharing options...
aeroswat Posted May 10, 2010 Share Posted May 10, 2010 Does $mysqli->query($query) not act as a constructor? If it doesn't then you are right. It's not truly OOP and they are using it the wrong way. Edit: When I say act as a constructor I mean does it call a constructor of its own and return a class object The method query() returns a MySQLi_STMT object, num_rows is a property that contains the number of rows by the last executed query. From what I'm reading on the php docs the query method returns a result object Return Values Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a result object. For other successful queries mysqli_query() will return TRUE. http://php.net/manual/en/mysqli.query.php Quote Link to comment Share on other sites More sharing options...
ignace Posted May 10, 2010 Share Posted May 10, 2010 From what I'm reading on the php docs the query method returns a result object I am well aware of what the query() method returns, thank you. And I am also more then familiar with the PHP.net manual, thank you. I just copy-pasted the wrong class name earlier. Quote Link to comment Share on other sites More sharing options...
aeroswat Posted May 10, 2010 Share Posted May 10, 2010 I am well aware of what the query() method returns, thank you. And I am also more then familiar with the PHP.net manual, thank you. I just copy-pasted the wrong class name earlier. No need to be rude about it. I wasn't trying to be. Just informing the correct information Quote Link to comment Share on other sites More sharing options...
ignace Posted May 10, 2010 Share Posted May 10, 2010 I ain't rude, I said thank you twice Quote Link to comment Share on other sites More sharing options...
aeroswat Posted May 10, 2010 Share Posted May 10, 2010 I ain't rude, I said thank you twice Usually when people say it like that they are trying to be rude I hope that sentence isn't sarcasm lol. Internet makes it hard to tell. Sorry if you weren't trying to be rude I just interpreted it as so. I apologize Quote Link to comment Share on other sites More sharing options...
programming.name Posted May 10, 2010 Author Share Posted May 10, 2010 Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a result object. For other successful queries mysqli_query() will return TRUE. If does the result object mean mysqli_result object(or class)? If then, it makes sense because up to "$mysqli->query($query)", it is the mysqli_result object( OR class??? ). And the class mysqli_result does have a property called "num_rows". So "$mysqli->query($query)->num_rows" sounds perfect. One final question, though, is when it says "blah blah blah will return a result object" what excatly would it be? I am a bit confused. Thanks Edit: What's my point is that if it returns a result object how are they formed? It is getting kind of hard-to-explain question. Sorry about that. Quote Link to comment Share on other sites More sharing options...
aeroswat Posted May 10, 2010 Share Posted May 10, 2010 Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a result object. For other successful queries mysqli_query() will return TRUE. If does the result object mean mysqli_result object(or class)? If then, it makes sense because up to "$mysqli->query($query)", it is the mysqli_result object( OR class??? ). And the class mysqli_result does have a property called "num_rows". So "$mysqli->query($query)->num_rows" sounds perfect. One final question, though, is when it says "blah blah blah will return a result object" what excatly would it be? I am a bit confused. Thanks What do you mean what exactly would it be? It's saying that the query method's return type is a mysqli_result object. mysqli_result is a class that contains the public method num_rows. There for what you are doing above with $mysqli->query($query) is creating a result object. Once that result object is created you are calling its public method with ->num_rows Quote Link to comment Share on other sites More sharing options...
programming.name Posted May 10, 2010 Author Share Posted May 10, 2010 I think I got what you mean. Thanks for your time. Thank you again! Quote Link to comment Share on other sites More sharing options...
ignace Posted May 10, 2010 Share Posted May 10, 2010 I think he has problems understanding the difference between a class and an object. The most common explanation they give, and in most cases gets the point across is that you can compare it to a blueprint of a house and a house itself respectively a class and an object. In simple terms: an object is created using the class as a blueprint. Quote Link to comment 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.