No error: PDO constructor was not called in
pdo, php
Solution
The problem is that you're extending the `PDO` class and overriding the constructor, all without calling the constructor.
Additionally, you're essentially creating two database connections every time you create a new object.
This should help resolve your issue, and reduce the connections created:
class EPDO extends PDO {
/** Some identificator of connection*/
public $db;
/**
* Creating new PDO connections
*/
public function __construct($dbhost, $dbname, $dbuser = 'root', $dbpass = '', $dbtype = 'mysql') {
parent::__construct($dbtype . ':host=' . $dbhost . ';dbname=' . $dbname, $dbuser, $dbpass);
}
/**
* Insert into database with using transaction (if operation failed the changes go before)
*/
public function insert($statement) {
$this->beginTransaction();
$status = $this->exec($statement);
if ($status) {
$this->commit();
} else {
$this->rollback();
}
}
}
Problem
Good afternoon. I'm starting using the PDO yesterday, and I have some problem with this. I was creating extendet class, which doesn't work and I can't find the bug. This is code of my helper class, for work witch PDO: ``` class EPDO extends PDO { /** Some identificator of connection*/ public $db; /** * Creating new PDO connections */ public function __construct($dbhost, $dbname, $dbuser = 'root', $dbpass = '', $dbtype = 'mysql') { $db = new PDO($dbtype . ':host=' . $dbhost . ';dbname=' . $dbname, $dbuser, $dbpass); } /** * Insert into database with using transaction (if operation failed the changes go before) */ public function insert($statement) { $db->beginTransaction(); $status = $db->exec($statement); if ($status) { $db->commit(); } else { $db->rollback(); } } } ``` And this is the unfunctional code: ``` $stm = $db->prepare('SELECT id FROM `startups` WHERE id = :id'); $params = array(':id' => $child->id); $ok = $stm->execute($params); $row = $stm->fetch(PDO::FETCH_ASSOC); ``` Before this code I of course call the connections following way: ``` require_once 'EPDO.php'; try { $db = new EPDO('--server--', '--database--', '--user--', '--pass--'); } catch (PDOException $err) { echo "Chyba spojeni: " . $err->getMessage(); } ``` Thank you very much, and sorry for my english.