php - PDO extend for a crud querys and Fatal error -
i'm trying make query class crud , want extend connection other class, error : fatal error: call member function prepare() on non-object !? miss ? or stupito extend ?
here mysql_connection extend querys class. both different php files.
class mysql_connection extends querys { protected $dbh = null; function __construct() { try { $this->dbh = new pdo('mysql:host=host; dbname=db','user','pass', array(pdo::mysql_attr_init_command=> 'set names utf8')); $this->dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception); $this->dbh->setattribute(pdo::attr_default_fetch_mode, pdo::fetch_assoc); } catch(pdoexception $e) { $this->dbh = null; print('error on connection'.$e->getmessage()); die(); } } }
and here query class
class querys{ function getdata() { $sql = "select * db"; $result = $dbh->prepare($sql); $result = execute(); $data = $result->fetch(pdo::fetch_assoc); return = $data; } }
you need call
$this->dbh->prepare($sql);
instead of
$dbh->prepare($sql);
you need replace
return = $data;
with
return $data;
remember return
statement, not variable!
furthermore, mysql_connection extends querys
incorrect use of inheritance. a extends b
means a
subtype of b
.
using real-world example, dog
subtype of animal
. because dog animal, right? dog extends animal
correct.
however, connection
not query
!
i recommend read bit inheritance , object oriented programming. ;)
Comments
Post a Comment