Skip to main content
Removing confidential data
Source Link
pollux1er
  • 5.8k
  • 5
  • 40
  • 37

I want to access a connexion database through PDO in a static way but I am still getting an error. This is my code :

require_once 'include/class.Settings.php';

class MonthlySums{
    protected static $dbh;
    public function __construct() {
        $user = Settings::$db['user'];
        $pass = Settings::$db['password'];
        $dsn = 'mysql:host=' . Settings::$db['host'] . ';dbname=' . Settings::$db['db'];
        
        try {
            self::$dbh = new PDO($dsn, $user, $pass);
        } catch(PDOException $e) {
            die("Error! : " . $e->getMessage());
        }
        
    }
    public static function get($init_id) {
        $sql = "SELECT `year`, `month`, `gains_monthly_sum` FROM `fxs_gain_sums` WHERE `init_id` = '$init_id'";
        $resultats = MonthlySums::$dbh->query($sql);
        var_dump($resultats);
    }
  
}

I have this error :

Fatal error: Call to a member function query() on a non-object in /home/fxscript/public_html/gainlosssums.php on line 45

That line is : $resultats = MonthlySums::$dbh->query($sql);

How to make it work a static way ?

I want to access a connexion database through PDO in a static way but I am still getting an error. This is my code :

require_once 'include/class.Settings.php';

class MonthlySums{
    protected static $dbh;
    public function __construct() {
        $user = Settings::$db['user'];
        $pass = Settings::$db['password'];
        $dsn = 'mysql:host=' . Settings::$db['host'] . ';dbname=' . Settings::$db['db'];
        
        try {
            self::$dbh = new PDO($dsn, $user, $pass);
        } catch(PDOException $e) {
            die("Error! : " . $e->getMessage());
        }
        
    }
    public static function get($init_id) {
        $sql = "SELECT `year`, `month`, `gains_monthly_sum` FROM `fxs_gain_sums` WHERE `init_id` = '$init_id'";
        $resultats = MonthlySums::$dbh->query($sql);
        var_dump($resultats);
    }
  
}

I have this error :

Fatal error: Call to a member function query() on a non-object in /home/fxscript/public_html/gainlosssums.php on line 45

That line is : $resultats = MonthlySums::$dbh->query($sql);

How to make it work a static way ?

I want to access a connexion database through PDO in a static way but I am still getting an error. This is my code :

require_once 'include/class.Settings.php';

class MonthlySums{
    protected static $dbh;
    public function __construct() {
        $user = Settings::$db['user'];
        $pass = Settings::$db['password'];
        $dsn = 'mysql:host=' . Settings::$db['host'] . ';dbname=' . Settings::$db['db'];
        
        try {
            self::$dbh = new PDO($dsn, $user, $pass);
        } catch(PDOException $e) {
            die("Error! : " . $e->getMessage());
        }
        
    }
    public static function get($init_id) {
        $sql = "SELECT `year`, `month`, `gains_monthly_sum` FROM `fxs_gain_sums` WHERE `init_id` = '$init_id'";
        $resultats = MonthlySums::$dbh->query($sql);
        var_dump($resultats);
    }
  
}

I have this error :

Fatal error: Call to a member function query() on a non-object in /home/public_html/gainlosssums.php on line 45

That line is : $resultats = MonthlySums::$dbh->query($sql);

How to make it work a static way ?

Source Link
pollux1er
  • 5.8k
  • 5
  • 40
  • 37

static and non static method to work

I want to access a connexion database through PDO in a static way but I am still getting an error. This is my code :

require_once 'include/class.Settings.php';

class MonthlySums{
    protected static $dbh;
    public function __construct() {
        $user = Settings::$db['user'];
        $pass = Settings::$db['password'];
        $dsn = 'mysql:host=' . Settings::$db['host'] . ';dbname=' . Settings::$db['db'];
        
        try {
            self::$dbh = new PDO($dsn, $user, $pass);
        } catch(PDOException $e) {
            die("Error! : " . $e->getMessage());
        }
        
    }
    public static function get($init_id) {
        $sql = "SELECT `year`, `month`, `gains_monthly_sum` FROM `fxs_gain_sums` WHERE `init_id` = '$init_id'";
        $resultats = MonthlySums::$dbh->query($sql);
        var_dump($resultats);
    }
  
}

I have this error :

Fatal error: Call to a member function query() on a non-object in /home/fxscript/public_html/gainlosssums.php on line 45

That line is : $resultats = MonthlySums::$dbh->query($sql);

How to make it work a static way ?