Reputation: 179
I am a newbie in PHP programming. I have created a config and a script which prints all my MySQL database information. Here is my 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, this is my script to get my database information :
require_once('config.php');
class ShowData extends DbManager{
public function getInfo(){
$dbh= getConnection();
$smt = $dbh->prepare("SELECT * FROM books");
$smt->execute();
$result = $smt->fetchAll();
echo "<pre>";
print_r($result);
}
}
I am getting the error : Fatal error: Uncaught Error: Call to undefined function getConnection()
.
I am not able to make a connection variable by which I can make my SQL queries. I have my suspicions about $dbh= getConnection()
. Am I making the queries alright?
Upvotes: 0
Views: 96
Reputation: 16436
Your getConnection
is in class so you need to call like $this->getConnection()
class ShowData extends DbManager{
public function getInfo(){
$dbh= $this->getConnection(); // change this line
$smt = $dbh->prepare("SELECT * FROM books");
$smt->execute();
$result = $smt->fetchAll();
echo "<pre>";
print_r($result);
}
}
Upvotes: 3