jasonwisdom Posted April 30, 2008 Share Posted April 30, 2008 Can PHP pass an object to a class method? I have a $mysqli object, representing the mysqli DB class of course. I am replacing mysql_pconnect() which used to exist at the top of every page. My classes just used the mysql_query() methods and similar....I want/need to replace these with $mysqli->multi_query(). But when I simply use $mysqli->multi_query(), I get an error. Fair enough...so I pass "$mysqli" as a parameter to the public methods. Now I don't get an error, but no actual results get returned. Any thoughts? Let me know if you want me to clarify. Thanks a bunch, Jason Quote Link to comment Share on other sites More sharing options...
trq Posted April 30, 2008 Share Posted April 30, 2008 Can PHP pass an object to a class method? Of course. Any thoughts? You'll need to post some code. Quote Link to comment Share on other sites More sharing options...
jasonwisdom Posted April 30, 2008 Author Share Posted April 30, 2008 This code is extremely stripped down....... class User { public $userid; public $access; function __construct() { $this->ResetValues(); } private function ResetValues() { $this->userid = 0; $this->access=$this->InitAccess(); } private function InitAccess() { $access = new UserAccess(); return $access; } public function LoadUser($userid, $mysqli) { echo "Now in load user.<br>"; $this->userid=$userid; $this->access->LoadAccess($userid,$mysqli); } } class UserAccess { public $hasuser; function __construct() { $this->hasuser=0; } public function LoadAccess($userid, $mysqli) { echo "Now in load access.<br>"; $sql="CALL spSelectUserAccess(".$userid.")"; if ($mysqli->multi_query($sql)) { echo "Now in SQL loop.<br>"; if($result = $mysqli->store_result()) { while($row = $result->fetch_array(MYSQLI_ASSOC)) { if($row['access']=="User") {$this->hasuser=1;} } } } } } When I run this..... I see "Now in load user." I see "Now in load access." I do not see "Now in SQL loop." Any ideas? Hope this is clear. Jason Quote Link to comment Share on other sites More sharing options...
jasonwisdom Posted May 2, 2008 Author Share Posted May 2, 2008 Anybody? Quote Link to comment Share on other sites More sharing options...
Mastodont Posted May 2, 2008 Share Posted May 2, 2008 if ($mysqli->multi_query($sql)) Try add else { $mysqli->error; } Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 2, 2008 Share Posted May 2, 2008 Chances are, the problem is that you are passing a resource by value, although your method of testing this is poor. For example, you're looking to discern something about how PHP oop works, based on the return value of a query that could fail. With that said, you might try this: public function LoadAccess($userid, &$mysqli) Quote Link to comment Share on other sites More sharing options...
aschk Posted May 6, 2008 Share Posted May 6, 2008 Passing objects without using the & (reference) symbol should work fine in php5+. So i doubt the issue lies here. Are you actually passing in the mysqli object? I see no code showing how you are calling these object methods, and thus i'm not sure if you're actually doing $user = new User(); $user->LoadUser(1, new MySqli("localhost", "my_user", "my_password", "world")); I'm assuming you're doing something like the above, but until I see it i can't rule out the fact that you might not even be giving it a valid mysqli object. According to the manual, mysqli->multi_query() returns a boolean result. Meaning that you're getting back FALSE from that query. So perhaps that user doesn't exist? Or the query is failling. I'm also guessing that stored procedures work ok in mysqli? 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.