jestem w trakcie przeprojektowania strony, konfiguracji i innych rzeczy i zmagam się z problemem od ponad godziny. A mianowicie mam dwa pliki - DatabaseConnector.php w którym łączę się z bazami danych oraz PlayerPanel.php.
Tak wygląda struktura pierwszego:
class DatabaseConnector { public $pdo; public $pdo2; public $host = "xxx"; public $port = 1234; public $username = "xxx"; public $password = "xxx"; public $database = "xxx"; public $database2 = "xxx"; public function __construct() { $this->connect(); } public function connect() { try { $this->pdo = new PDO('mysql:host='.$this->host.';dbname='.$this->database.';port='.$this->port, $this->username, $this->password ); $this->pdo2 = new PDO('mysql:host='.$this->host.';dbname='.$this->database2.';port='.$this->port, $this->username, $this->password ); } catch(PDOException $e) { exit; } } public function __destruct() { $this->closeConnection(); } public function closeConnection() { $this->pdo = null; $this->pdo2 = null; } }
PlayerPanel.php
include "DatabaseConnector.php"; class PlayerPanel extends DatabaseConnector { public $nick; public $kills; public $deaths; public $kd; public $guild; public function __construct($id) { if($id == null) { } $this->nick = $id; } public function getPlayerKills() { return $this->kills; } public function getPlayerDeaths() { return $this->deaths; } public function getKDRatio() { return $this->kd; } public function getGuild() { return $this->guild; } public function getPlayerInfo($tag) { $stmt = $this->pdo2 -> prepare("SELECT * FROM `XXXX` WHERE player=:tag"); $stmt -> bindValue(':tag', $tag, PDO::PARAM_STR); // 2 $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_COLUMN, 0); return $result; } public function getPlayerGuild($nick) { $stmt = $this->pdo2 -> prepare("SELECT * FROM `XXXX` WHERE player=:tag"); $stmt -> bindValue(':tag', $nick, PDO::PARAM_STR); // 2 $stmt->execute(); $results=$stmt->fetchAll(PDO::FETCH_ASSOC); $json=json_encode($results); return $json; } }
Chcę sobie wyświetlić statystyki gracza metodą echo $this->getPlayerInfo($this->nick);, ale wygląda, że to wcale nie działa.
Próbowałem różnych metod, cały czas mój php twierdzi, że $this->pdo2 nie istnieje... błąd wygląda następująco: Fatal error: Uncaught Error: Call to a member function prepare() on null in .... 54 (pogrubiłem to w kodzie)