0

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 ?

10
  • change MonthlySums::$dbh->query($sql); to self::$dbh->query($sql); Commented Apr 15, 2016 at 10:10
  • Fatal error: Access to undeclared static property: Settings::$dbh in Commented Apr 15, 2016 at 10:10
  • Ok, the previous error is back again Commented Apr 15, 2016 at 10:12
  • Why have it static in the first place? Commented Apr 15, 2016 at 10:12
  • 1
    When you call a static method in a class the constructor is not automatically invoked like when you create an instance of the class. With your code you would have to create at least once instance of the class so that the database connection would be set in your static variable. Then you would be able to call it in your desired static way. Commented Apr 15, 2016 at 10:25

1 Answer 1

1

When you call a static method in a class, the constructor is not automatically invoked like when you create an instance of the class. With your code you would have to create at least one instance of the class so that the database connection would be set in your static variable. Then you would be able to call it in your desired static way.

If you still want to continue with your code, you could change it to this:

(Please note: this is the OP's modified code and I am not going into other aspects - just the result to the OP's question/problem)

require_once 'include/class.Settings.php'; class MonthlySums{ protected static $dbh; public function __construct() { $this->databaseLogin(); } public static function databaseLogin(){ $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 = self::$dbh->query($sql); var_dump($resultats); } } MonthlySums::databaseLogin(); 

As you can see there is a new static method databaseLogin and is called right after the class declaration. This way the static variable for the database connection is set and can be used in other methods.

You now can call it this way:

MonthlySums::get(1234); 

Again, this might not be best practice but a solution to your problem.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.