mostafatalebi Posted November 30, 2012 Share Posted November 30, 2012 fatal error Call to a member function prepare() on a non-object I get this error. I don't know how to solve it? I instance it from this code: <?php $something = new login_table("localhost", "root", "", "object"); $final = $something->queryStatement("SELECT * FROM table"); print_r($final); ?> Here is the main class: <?php class login_table { protected $_connect; protected $_queryStatemet; function __construct($host, $user, $pass, $database) { if(!$_connect = new mysqli($host, $user, $pass, $database)) { echo "Problem in connecting to the database."; } } function queryStatement($_queryStatemet) { $this->_queryStatemet = filter_var($_queryStatemet, FILTER_SANITIZE_STRING); $stmt = $this->_prepareStatement(); $stmt->execute(); $stmt->store_result; return $stmt; } protected function _prepareStatement() { if(!$ps = $this->_connect->[b]prepare([/b]$this->_queryStatemet)) { die("No preparation."); } return $final; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/271424-fatal-error-call-to-a-member-function-prepare-on-a-non-object/ Share on other sites More sharing options...
kicken Posted November 30, 2012 Share Posted November 30, 2012 (edited) You never assign your class's _connect variable. This line: if(!$_connect = new mysqli($host, $user, $pass, $database)) should be: if(!$this->_connect = new mysqli($host, $user, $pass, $database)) Edited November 30, 2012 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/271424-fatal-error-call-to-a-member-function-prepare-on-a-non-object/#findComment-1396566 Share on other sites More sharing options...
mostafatalebi Posted December 1, 2012 Author Share Posted December 1, 2012 Hey man thank you it was very foolish mistake. now it is done. thanks Quote Link to comment https://forums.phpfreaks.com/topic/271424-fatal-error-call-to-a-member-function-prepare-on-a-non-object/#findComment-1396628 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.