I am trying to build a PDO connection to my MySQL database. This PDO is also being inherited by the child-classes of this class, where connection-specific functions are called from. When I call these connection-specific functions from the child-classes, I get an error.
So within the parent class, I have the following code:
...
protected $pdo;
public function __constructor() { //Check if public
try {
$pdo_local = new PDO(C_DB_PDODRIVER . ":host=" . C_DB_HOST . ";dbname=" . C_DB_DATABASE, C_DB_USERNAME, C_DB_PASSWORD);
$this->pdo = $pdo_local;
} catch (\PDOException $e) {
header("Location: error.html");
exit;
}
}
...
However, whenever I try to call the prepare statement of $this->pdo, I get a "Call to a member function prepare()" fatal error.
$query = "SELECT * FROM stacks WHERE id = :id LIMIT 1";
$stm = $this->pdo->prepare($query);
$para = array("id"=>$id); //where $id is the input parameter of the function
$stm->execute($para);
$result = $stm->fetch();
I have read that the PDO data-object is a null object, but I don't understand why? shouldn't $this->pdo = $pdo_local change that?
I am quite new to StackOverflow, so sorry if this question has flaws. Also, If you see any possible improvements in my code, please tell me!
Proper function name for constructor is __construct
.