Page 1 of 1

Fatal error: Call to a member function on a non-object

Posted: Sun Nov 22, 2009 8:53 am
by sheppardzwc
Hey everyone,

I'm designing a member database for a friend of mine's video game site. I'm using OOPHP. I'm just now seeing if my trial and error coding is paying off when I start getting these errors..

Fatal error: Call to a member function getNumRows() on a non-object in /home/will/public_html/g/includes/class.setting.php on line 17

The files are as follows:

global.php (located in a directory below)

Code: Select all

<?php
 
########################################
#||||||||||||||||||||||||||||||||||||||#
#|| TheGuild v2.0                    ||#
#|| Redistributable Website Software ||#
#|| Copright 2009, TheGuild Ltd.     ||#
#||||||||||||||||||||||||||||||||||||||#
########################################
 
    #####################
    # Intialitize Files #
    #####################
    
    require_once('includes/class.db.php');
    require_once('includes/class.security.php');
    require_once('includes/class.setting.php');
    require_once('includes/class.html.php');
        require_once('includes/class.html.template.php');
    require_once('includes/config.php');
    require_once('includes/class.cookie.php');
    
    #####################
    # Construct Classes #
    #####################
    
        $setting = new SettingManager();
        $db = new DB($setting->getConfigSetting('DBHOST'), $setting->getConfigSetting('DBUSER'), $setting->getConfigSetting('DBPASS'));
            $db->connect();
        $security = new SecurityManager();
        $html = new HTMLFactory();
        $template = new TemplateManager();
        $error = new ErrorFactory();
        $cookie = new CookieFactory();
    
?>
class.db.php

Code: Select all

<?php
 
########################################
#||||||||||||||||||||||||||||||||||||||#
#|| TheGuild v2.0                    ||#
#|| Redistributable Website Software ||#
#|| Copright 2009, TheGuild Ltd.     ||#
#||||||||||||||||||||||||||||||||||||||#
########################################
 
class DB {
 
    public function __construct($host, $user, $pass, $dbname) {
        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;
        $this->name = $dbname;
    }
    
    public function connect() {
        if(!$connection) {
            if($setting->getConfigSetting('DBPORTENABLED')) {
                $connection = mysql_connect($this->host . ':' . $setting->getConfigSetting('DBPORT'), $this->user, $this->pass);
            } else {
                $connection = mysql_connect($this->host, $this->user, $this->pass);
            }
        }
        if(!$connection) {
            $error->returnPrepared('cannot_connect');
        } else if(!mysql_select_db($this->name)) {
            $error->returnPrepared('cannot_connect');
        } else {
            return $connection;
        }
    }
    
    public function query($query) {
        $this->q = $query;
        $this->qv = mysql_query($this->q);
            if(!$this->qv) {
                $error->returnPrepared('query_error');
            }
        return $this->qv;
    }
    
    public function getUserFromID($username) {
        $this->getiduser = $username;
        $this->id = $db->fetchArray($db->query("SELECT userid FROM accounts WHERE username='$this->getiduser'"));
        return $this->id['userid'];
    }
    
    public function getArray($array) {
        $this->arr = $array;
        return mysql_fetch_array($this->arr);
    }
    
    public function getNumRows($numrows) {
        $this->rows = $numrows;
        return mysql_num_rows($this->rows);
    }
    
}
 
?>
class.setting.php

Code: Select all

<?php
 
########################################
#||||||||||||||||||||||||||||||||||||||#
#|| TheGuild v2.0                    ||#
#|| Redistributable Website Software ||#
#|| Copright 2009, TheGuild Ltd.     ||#
#||||||||||||||||||||||||||||||||||||||#
########################################
 
require_once('../global.php');
 
class SettingManager {
    
    private function settingExists($setting) {
        $this->existssettingname = $setting;
        if($db->getNumRows($db->query("SELECT * FROM settings WHERE name='$this->existssettingname'")) >= 1) {
            return true;
        } else {
            return false;
        }
    }
    
    public function getConfigSetting($setting) {
        $this->getconfigsettingname = $setting;
        if($this->settingExists($this->getconfigsettingname)) {
            $this->configsettingarray = array('DBHOST'                => $config['db']['host'],
                                              'DBUSER'                => $config['db']['username'],
                                              'DBPASS'                => $config['db']['password'],
                                              'DBPORT'                => $config['db']['port'],
                                              'DBPORTENABLED'         => $config['db']['portenabled'],
                                              'TOONLIMIT'             => $config['toon']['toonlimit'],
                                              'IPLIMIT'               => $config['toon']['iplimit'],
                                              'BOARDMEMBER'           => $config['toon']['boardmember'],
                                              'COOKIELIMIT'           => $config['server']['cookielimit']);
            if(in_array($this->getsettingname, $this->configsettingarray)) {
                return $this->configsettingarray[$this->getsettingname];
            } else {
                return 0;
                $error->returnPrepared('get_setting_error');
            }
        } else {
            return 0;
            $error->returnPrepared('get_setting_error');
        }
    }
    
    public function getSetting($setting) {
        $this->getsettingname = $setting;
        if($this->settingExists($this->getsettingname)) {
            $this->settingarray = array('META-DESCRIPTION'      => "SELECT value FROM settings WHERE name='metadesc'",
                                        'META-KEYWORDS'         => "SELECT value FROM settings WHERE name='metakeywords'",
                                        'META-CHARSET'          => "SELECT value FROM settings WHERE name='metacharset'");
            if(in_array($this->getsettingname, $this->settingarray)) {
                $this->setting = $db->getArray($db->query($this->settingarray[$this->getsettingname]));
                return $this->setting['value'];
            } else {
                return 0;
                $error->returnPrepared('get_setting_error');
            }
        } else {
            return 0;
            $error->returnPrepared('get_setting_error');
        }
    }
    
}
 
?>
What am I doing wrong here?

Re: Fatal error: Call to a member function on a non-object

Posted: Sun Nov 22, 2009 9:00 am
by BlaineSch
"$db" is not defined in "SettingManager::settingExists"

Re: Fatal error: Call to a member function on a non-object

Posted: Sun Nov 22, 2009 9:57 am
by sheppardzwc
BlaineSch wrote:"$db" is not defined in "SettingManager::settingExists"
How do I define it without having to do that in every single function? I thought when you included 'global.php' that it loaded all the "$db = new DB();" in each file?

Re: Fatal error: Call to a member function on a non-object

Posted: Sun Nov 22, 2009 10:02 am
by iankent
I think you just need to add:

Code: Select all

global $db;
after line 15 in class.setting.php.

To refer to a global variable within a function you have to use global $whatever so PHP knows you're referring to the global one.

hth