i newbie in php programming. have created config , script prints mysql database information. here config :
class dbmanager{ protected $dbh; public $dbhost = '127.0.0.1'; public $dbname = 'bookstore'; public $dbuser = 'root'; public $dbpass = ''; public function getconnection(){ try{ $dbh = new pdo("mysql:host=$this->dbhost;dbname=$this->dbname", $this->dbuser, $this->dbpass); $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception); return $dbh; } catch(pdoexception $e){ echo "error : " . $e; } }
} and, script database information :
require_once('config.php'); class showdata extends dbmanager{ public function getinfo(){ $dbh= getconnection(); $smt = $dbh->prepare("select * books"); $smt->execute(); $result = $smt->fetchall(); echo "<pre>"; print_r($result); } }
i getting error : fatal error: uncaught error: call undefined function getconnection()
. not able make connection variable can make sql queries. have suspicions $dbh= getconnection()
. making queries alright?
your getconnection
in class need call $this->getconnection()
class showdata extends dbmanager{ public function getinfo(){ $dbh= $this->getconnection(); // change line $smt = $dbh->prepare("select * books"); $smt->execute(); $result = $smt->fetchall(); echo "<pre>"; print_r($result); } }
Comments
Post a Comment