This question already has an answer here:
I seem to be having some issues loading my site using a PDO class I found here:
http://culttt.com/2012/10/01/roll-your-own-pdo-php-class/
I have built the code on a local MAMP server using php 5.5.18 and everything seems to work fine, however when I upload it to my web server I get the following error:
PHP Fatal error: Call to a member function prepare() on a non-object in /global/classes.php on line 51
I have compared phpinfo() on both my localhost and server and I can't see any major discrepancies in configurations different directory paths and the odd extension version being .1-.3 versions older than the localhost are the main differences however both servers are using php 5.5.18.
The class that I am using is:
//Define Database Vars based on Site Mode
if($config_sitemode=='dev'){
define("DB_HOST", $config_devdbhost);
define("DB_USER", $config_devdbuser);
define("DB_PASS", $config_devdbpass);
define("DB_NAME", $config_devdbname);
define("DB_PREFIX", $config_devdbprefix);
}
elseif($config_sitemode=='live'){
define("DB_HOST", $config_livedbhost);
define("DB_USER", $config_livedbuser);
define("DB_PASS", $config_livedbpass);
define("DB_NAME", $config_livedbname);
define("DB_PREFIX", $config_livedbprefix);
}
else{
echo 'Error Connecting to Database: Invalid Site Mode';
}
// Database Class
class Database{
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $dbh;
private $error;
private $stmt;
public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function rowCount(){
return $this->stmt->rowCount();
}
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
public function beginTransaction(){
return $this->dbh->beginTransaction();
}
public function endTransaction(){
return $this->dbh->commit();
}
public function cancelTransaction(){
return $this->dbh->rollBack();
}
public function debugDumpParams(){
return $this->stmt->debugDumpParams();
}
}
Line 50, 51, 52 are:
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
When I am calling the database I am using this code:
$database = new Database();
$database->query('SELECT firstname, surname, username, role FROM '.DB_PREFIX.'staffprofiles WHERE sid = :sid');
$database->bind(':sid', $sid);
$result = $database->single();
If anyone is able to help me and shed some light on to why I might be receiving these errors I would appreciate your help
Edit: Problem solved, had to look closer at where I defined the database variables and it pointed me to a typo.
0 comments:
Post a Comment