i am trying to use PDO in php using classes.
so i have defined one config file like that
<?php
global $configVars;
$configVars['online'] = false;
if(($_SERVER['SERVER_NAME'])!='localhost' and ($_SERVER['SERVER_NAME'])!='abc')
{
$configVars['dbhost'] = "localhost";
$configVars['dbuser'] = "dbuser";
$configVars['dbpassword'] = "dbpass";
$configVars['dbname'] = "dbname";
}
?>
then i have defined class to connect the database
<?php
class dbClass{
var $dbHost,
$dbUser,
$dbName,
$dbPass,
$dbTable,
$dbLink,
$resResult,
$dbh,
$dbPort;
function dbClass(){
global $configVars;
$this->dbHost = $configVars['dbhost'];
$this->dbUser = $configVars['dbuser'];
$this->dbPass = $configVars['dbpassword'];
$this->dbName = $configVars['dbname'];
$this->connect_PDO();
}
function connect_PDO(){
try {
$dbh = new PDO('mysql:host='.$this->dbHost.';dbname='.$this->dbName.'', $this->dbUser, $this->dbPass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected.";
}
catch(PDOException $e) {
echo "I'm sorry Charlie, I'm afraid i cant do that.";
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
$dbh = null;
}
}
}
?>
now i have defined another class to select the records from table
<?
class cms extends dbClass
{
function get_rec($id=0)
{
($id==0?$addQuery="":$addQuery=" where id =".$id);
$statement = $dbh->prepare("select * from TABLENAME :name order by id");
$statement->execute(array(':name' => $addQuery));
$row = $statement->fetchAll();
return $row ;
}
}
?>
now when i am using this function in my PHP file like this
<?php
$objCMS=new cms();
$getCMS=$objCMS->get_rec(1);
echo $getCMS[0];
?>
i got following out put
Connected.
Fatal error: Call to a member function prepare() on a non-object in /Applications/XAMPP/xamppfiles/htdocs
i am trying to sort this out from last 2 days but no success.
i am new to PDO and want to use it.
Thanks