pcman Posted December 10, 2010 Share Posted December 10, 2010 i am tring to extend from DataAccess to Module... but is showing an error: location: Library/database.php <?php Class DatabaseAccess { public $db; public $sqlQuery; public function ConnectDB($host,$user,$password,$database){ $this->db = mysqli_connect($host,"",$password); if (mysqli_connect_errno()) { echo '<p>Cannot connect to DB: ' . mysqli_connect_error() . '</p>'; } $this->db &= mysqli_select_db($this->db,$database); } public function SqlQuery($sql) { return mysqli_query($this->db,$sql);//or die("Error:".mysqli_error()."<br />The Query:<b>$sql</b>"); } } ?> The Module class Location:Library/module.php <?php Class Module extends DatabaseAccess { public function DoInsert($tableName,$fields = array(),$values = array())//delete from table { $fields = implode(",", $fields); $values = implode("','", $values); return $this->db->SqlQuery ("INSERT INTO $tableName($fields) VALUES('$values')");//The error is here!!!!!!!!!!!!!!!! } } ?> and the controller file name: newProfile.php this is the cdode include "config.php"; include "Library/database.php"; include "Library/module.php"; $db = new DatabaseAccess; $db->ConnectDB(MYSQL_HOST_NAME,MYSQL_USER_NAME,MYSQL_PASSWORD,MYSQL_DATABASE);//define in config.php $db->Module = New Module; $db->Module->DoInsert("twitter_profiles", array(profile_user_name,profile_password,user_id), array($_POST['userName'],$_POST['password'],$userID));//DoInsert(tableName,Fields,Values) echo sysMessage("NewProfile.txt"); the error: Fatal error: /home/omerbsh/blalalala.com/tra/Library/module.php on line 59 the error is in the modle class where the comment: "The error is here!!!!!!!!!!!!!!!!" its cant to fine SqlQuery object but why??? thanks Quote Link to comment https://forums.phpfreaks.com/topic/221172-call-to-a-member-function-sqlquery-on-a-non-object-in/ Share on other sites More sharing options...
Buddski Posted December 10, 2010 Share Posted December 10, 2010 Because your Module class is extending the DatabaseAccess class you can access the function using return $this->SqlQuery ("INSERT INTO $tableName($fields) VALUES('$values')"); $this->db is a resource not the class Let me know how you go. Quote Link to comment https://forums.phpfreaks.com/topic/221172-call-to-a-member-function-sqlquery-on-a-non-object-in/#findComment-1145174 Share on other sites More sharing options...
trq Posted December 10, 2010 Share Posted December 10, 2010 You calling code is weird too, you might want to take another look at how inheritance actually works. include "config.php"; include "Library/database.php"; include "Library/module.php"; $db = new Module; $db->ConnectDB(MYSQL_HOST_NAME,MYSQL_USER_NAME,MYSQL_PASSWORD,MYSQL_DATABASE);//define in config.php $db->DoInsert("twitter_profiles", array(profile_user_name,profile_password,user_id), array($_POST['userName'],$_POST['password'],$userID) );//DoInsert(tableName,Fields,Values) echo sysMessage("NewProfile.txt"); Quote Link to comment https://forums.phpfreaks.com/topic/221172-call-to-a-member-function-sqlquery-on-a-non-object-in/#findComment-1145191 Share on other sites More sharing options...
Anti-Moronic Posted December 10, 2010 Share Posted December 10, 2010 You calling code is weird too, you might want to take another look at how inheritance actually works. include "config.php"; include "Library/database.php"; include "Library/module.php"; $db = new Module; $db->ConnectDB(MYSQL_HOST_NAME,MYSQL_USER_NAME,MYSQL_PASSWORD,MYSQL_DATABASE);//define in config.php $db->DoInsert("twitter_profiles", array(profile_user_name,profile_password,user_id), array($_POST['userName'],$_POST['password'],$userID) );//DoInsert(tableName,Fields,Values) echo sysMessage("NewProfile.txt"); Could you explain what doesn't add up here in his code? Sure it would help him more and also for my own curiosity. Quote Link to comment https://forums.phpfreaks.com/topic/221172-call-to-a-member-function-sqlquery-on-a-non-object-in/#findComment-1145193 Share on other sites More sharing options...
pcman Posted December 10, 2010 Author Share Posted December 10, 2010 you have another idea how to call to DatabaseAccess , and why Module class dosent extends from DatabaseAccess class? Quote Link to comment https://forums.phpfreaks.com/topic/221172-call-to-a-member-function-sqlquery-on-a-non-object-in/#findComment-1145277 Share on other sites More sharing options...
Buddski Posted December 10, 2010 Share Posted December 10, 2010 As thorpes post says, your inheritance is a little bit wacky.. By calling Module class it will inherit all the stuff inside the DatabaseAccess class as it is extended from it.. You version, I believe, creates a DatabaseAccess object then creates a 'Module' within that class, in doing so, the Module object inside your DatabaseAccess object will inherit from DatabaseAccess and essentially you will have 2 DatabaseAccess objects available to your script (at least I think so) Quote Link to comment https://forums.phpfreaks.com/topic/221172-call-to-a-member-function-sqlquery-on-a-non-object-in/#findComment-1145524 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.