grasshopper31 Posted May 25, 2014 Share Posted May 25, 2014 Hello guys, i'm currently building my own cms, a personal project, and now im stucked on an error "Call to a member function query() on a non-object in.. please help after creating this function.. I know the db connection and everything else worked out because i have a similar function that works just without the switch or the numrow if statement. protected function _pageStatus($option, $id){ //check if page exists, if it does return the status, or return 404 switch($option){ case 'alpha' : $sql = "SELECT status FROM pages WHERE nick = '$id'"; break; case 'num' : $sql = "SELECT status FROM pages WHERE id = '$id'"; break; } if($result = $this->_db->query($sql)){ //<--- THE ERROR WAS ON THIS LINE. if($result->num_rows > 0){ while ($status = $result->fetch_object()) { return $status; } return $status; $result->close(); } else { return 404; } } } Quote Link to comment https://forums.phpfreaks.com/topic/288768-mysqli-query-problem/ Share on other sites More sharing options...
Jacques1 Posted May 25, 2014 Share Posted May 25, 2014 Hi, whatever $this->_db contains, it's not an object. Are you sure you've initialized the attribute? Quote Link to comment https://forums.phpfreaks.com/topic/288768-mysqli-query-problem/#findComment-1480831 Share on other sites More sharing options...
grasshopper31 Posted May 25, 2014 Author Share Posted May 25, 2014 yes, the script grabs data from db for other things using $this->_db.. here is the file. <?php if(!defined('ACCESS_CORE')){ echo 'Not today..'; exit; } class mod_global{ protected $_db; var $_id; var $_www; var $_url; public $_settings; public $_page; public $_year; protected $_status; function __construct(){ //load global variables $this->_id = $this->_pageSelect(); $this->_year = date('Y'); $this->_url = 'http://localhost/'; $this->_www = explode('core', __DIR__); #get abspath to rootdir #TODO: find a better method $this->_www = $this->_www[0]; echo $this->_id; // load required methods $this->_db(); $this->_loadSettings(); $this->_loadContent(); } protected function _db(){ $db = parse_ini_file('../contra.conf'); $this->_db = new mysqli($db['host'], $db['user'], $db['pass'], $db['name']); if ($this->_db->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } } protected function _pageSelect(){ #user may enter a nicknamed pages -> data, Apps or a numbered page -> data if(count($_GET) > 0 && !empty($_GET['id']) && $_GET['id'] !== 0){ //a request, id isnt empty or 0 $id = $this->_inputCheck('id', $_GET['id']); //filter.. alphanumeric only if(!is_numeric($id) && $this->_pageStatus('alpha', $id) !== 404){ $real_id = $id; } else if(is_numeric($id) && $this->_pageStatus('num', $id) !== 404){ $real_id = $id; } } else { //home requested $real_id = 0; } return $real_id; } protected function _pageStatus($option, $id){ //check if page exists, if it does return the status, or return 404 switch($option){ case 'alpha' : $sql = "SELECT status FROM pages WHERE nick = '$id'"; break; case 'num' : $sql = "SELECT status FROM pages WHERE id = '$id'"; break; } if($result = $this->_db->query($sql)){ if($result->num_rows > 0){ while ($status = $result->fetch_object()) { return $status; } return $status; $result->close(); } else { return 404; } } } protected function _inputCheck($type, $var){ switch($type){ case 'id' : $result = preg_replace("/[^a-z0-9]+/", "", $var); $result = mysql_real_escape_string($result); break; //leaves alphanumeric case 'html' : $result = mysql_real_escape_string($var); #TODO: improve the html, also sanitize in the form break; //escapes } return $result; } protected function _loadSettings(){ $sql = 'SELECT * FROM settings'; if($result = $this->_db->query($sql)){ /* fetch object array */ while ($settings = $result->fetch_object()) { $this->_settings = $settings; } /* free result set */ $result->close(); } } protected function _loadContent(){ if(!is_numeric($this->_id)){ $sql = "SELECT * FROM pages WHERE nick = '$this->_id'"; //sql for alpha nick id } else if(is_numeric($this->_id)){ $sql = "SELECT * FROM pages WHERE id = '$this->_id'"; //sql for numeric id } if($result = $this->_db->query($sql)){ /* fetch object array */ while ($pageContent = $result->fetch_object()) { $this->_page = $pageContent; } /* free result set */ $result->close(); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/288768-mysqli-query-problem/#findComment-1480833 Share on other sites More sharing options...
mogosselin Posted May 26, 2014 Share Posted May 26, 2014 Try using trigger_error() instead of "echo" when you have an error. Because when you echo errors like that and an error occurs, the app will be in an unknown states and the code will continue anyway, meaning that you'll get a bug latter that is harder to find. And, it's also possible that the "echoed" error message won't be visible, depending on where it is printed in the HTML code. Try to print the value of the $db array that is initialized with the file "contra.conf", I suspect that there is an error there... And change that "echo "Failed to connect... " for a trigger_error. You could also add this at the end of the _db() method or just before calling $this->_db->query($sql). That way you'll see if it's an object or a variable (probably holding the value false from an error somewhere?) if (!is_object($this->_db)) { trigger_error("DB isn't an object!", E_USER_ERROR); } Quote Link to comment https://forums.phpfreaks.com/topic/288768-mysqli-query-problem/#findComment-1480840 Share on other sites More sharing options...
grasshopper31 Posted May 26, 2014 Author Share Posted May 26, 2014 (edited) Changing to trigger_error() seems like a fine advice and it just makes sense, and there is no problem with my db connection for when I use _loadSettings(); it works. Also when i remove the if statements within _pageStatus(); so that the function does not have affect the script at all. I can retrieve data from db using $this->_db(). Also after adding the code you suggested in my _db() function does not give me the error that is not an object. So something must be contradicting in the script when i add this to the _pageStatus() method in the file or maybe im just doing this using the wrong php ways. EDIT: when i visit my home "localhost" it seems to work just fine no errors, the problem is when i switch to other pages thru ?id= but again it does work and retrieves data normally when i remove the code below. if($result = $this->_db->query($sql)){ if($result->num_rows > 0){ while ($status = $result->fetch_object()) { return $status; } return $status; $result->close(); } else { return 404; } } Edited May 26, 2014 by grasshopper31 Quote Link to comment https://forums.phpfreaks.com/topic/288768-mysqli-query-problem/#findComment-1480843 Share on other sites More sharing options...
Solution grasshopper31 Posted May 26, 2014 Author Solution Share Posted May 26, 2014 FIXED: I just had to call the _db() method first before $this->_id which makes sense i knew it was something obvious and that i just irritated looking in the wrong place. function __construct(){ $this->_db(); //load global variables $this->_id = $this->_pageSelect(); $this->_year = date('Y'); $this->_url = 'http://localhost/'; $this->_www = explode('core', __DIR__); #get abspath to rootdir #TODO: find a better method $this->_www = $this->_www[0]; echo $this->_id; // load required methods $this->_loadSettings(); $this->_loadContent(); } Quote Link to comment https://forums.phpfreaks.com/topic/288768-mysqli-query-problem/#findComment-1480846 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.