Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No error: PDO constructor was not called in

Tags:

php

pdo

I was creating extended class from PDO, which doesn't work and I can't find the bug.

This is code of my helper class:

    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 non-functional 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();
  }

This line call to me (on line 15 is variable $stm):

Warning: PDO::prepare() [pdo.prepare]: SQLSTATE[00000]: No error: PDO constructor was not called in /data/web/virtuals/2286/virtual/www/subdom/map/sm/GetStartups.php on line 15

Fatal error: Call to a member function execute() on a non-object in /data/web/virtuals/2286/virtual/www/subdom/map/sm/GetStartups.php on line 17

like image 699
user3049124 Avatar asked Mar 12 '26 07:03

user3049124


2 Answers

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();
        }
    }
}
like image 155
garbetjie Avatar answered Mar 14 '26 22:03

garbetjie


Two problems: your design is wrong. You extend PDO, yet you create a new member object $db of type PDO inside. That won't work as you intend it. You'll have to call its super constructor.

public function __construct($dbhost, $dbname, $dbuser = 'root', $dbpass = '', $dbtype = 'mysql') {
      parent::__construct($dbtype . ':host=' . $dbhost . ';dbname=' . $dbname, $dbuser, $dbpass);
} 

In fact, this constructor isn't needed at all, you are just changing the parameters, creating confusion. So you can easily remove it, together with the $db attribute.

Second, to refer to class members and methods, use $this in PHP:

public function insert($statement) {
    $this->beginTransaction();

    $status = $this->exec($statement);
    if ($status) {
      $this->commit();  
    } else {
      $this->rollback();
    }
} 
like image 31
Bart Friederichs Avatar answered Mar 14 '26 22:03

Bart Friederichs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!