How can one access PDO’s execute() method from inside a db class? Below is a snippet of my class and method:
class db extends PDO {
public $memcache;
function execute($params='') {
try {$foo=parent::execute($params);}
catch (PDOException $e) {echo $e->getMessage();}
return $foo;
}
function __construct($db='server_en',$host='localhost',$uz='root',$pw='') {
try {
parent::__construct("mysql:dbname=$db;host=$host",$uz,$pw);
parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
catch (PDOException $e) {echo $e->getMessage();}
$this->memcache=new Memcache;
$this->memcache->connect('127.0.0.1', 11211) or die('Could not connect to Memcache server');
}
}
I have tried executing the following code:
var_dump($db->execute('INSERT INTO `foo`(`bar`) VALUES (0)'));
And got the following error: Fatal error: Call to undefined method PDO::execute() in ....
Could anyone please supply a simple example of how this can be fixed? I am fairly inexperienced with both PDO and classes, so the simpler the answer the better.
P.S. The reason for me replicating some of PDO’s methods is easier migration to a new db handler when PDO becomes outdated. This is not premature optimization; this is making life easier from the start.
You can only call
::execute()on a PDOStatement, not PDO itself.