I am getting an error as follows:
Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\xampp\htdocs\newsportal\class\Database.php:81
Stack trace:
#0 C:\xampp\htdocs\newsportal\class\User.php(9): Database->select(Array)
#1 C:\xampp\htdocs\newsportal\admin\process\login.php(10): User->getUserByUserName('admin@newsporta...')
#2 {main} thrown in C:\xampp\htdocs\newsportal\class\Database.php on line 81
And my code is:
<?php
abstract class Database {
private $conn = null;
private $stmt = null;
private $sql = null;
private $table = null;
public function __construct() {
try {
$this->conn = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.";", DB_USER, DB_PWD);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//for unicode character
$this->stmt = $this->conn->prepare("SET NAMES utf8");
$this->stmt->execute();
// for unicode character
}
catch(PDOException $e) {
error_log(date('Y-m-d h:i:s A') . ":" . $e->getMessage() . "\n", 3, ERROR_PATH . "error.log");
return false;
}
catch(Exception $e) {
error_log(date('Y-m-d h:i:s A') . ":" . $e->getMessage() . "\n", 3, ERROR_PATH . "error.log");
return false;
}
}
final protected function table($_table) {
$this->table = $_table;
}
final protected function select($args = array(), $is_die = false) {
try {
// /* SELECT field_name*/
$this->sql = "SELECT ";
if (isset($args['fields']) && !empty($args['fields'])) {
if (is_array($args['fields'])) {
$this->sql.= implode(" , ", $args['fields']); //implode converts array to string with , glue
} else {
$this->sql.= $args['fields']; //not array's case
}
} else {
$this->sql.= "*";
}
// /* SELECT field_name*/
// /*FROM table_name*/
$this->sql.= " FROM ";
if (!isset($this->table) || $this->table == null) {
throw new Exception('Table not set.');
}
$this->sql.= $this->table;
// /*FROM table_name*/
/*JOIN*/
/*JOIN*/
/*WHERE*/
if (isset($args['where']) && !empty($args['where'])) {
if (is_array($args['where'])) {
/*loop*/
$temp = array();
foreach ($args['where'] as $column_name => $value) {
$str = $column_name." = : ".$column_name;
$temp[] = $str;
}
$this->sql.= " WHERE ". implode(' AND ', $temp);
} else {
$this->sql.= "WHERE" . $args['where'];
}
}
/*WHERE*/
if ($is_die) {
debug($args);
echo $this->sql;
exit;
}
$this->stmt = $this->conn->prepare($this->sql);
if (isset($args['where']) && is_array($args['where'])) {
foreach ($args['where'] as $column_name => $value) {
if (is_integer($value)) {
$param = PDO::PARAM_INT;
} else if (is_bool($value)) {
$param = PDO::PARAM_BOOL;
} else {
$param = PDO::PARAM_STR;
}
if ($param) {
$this->stmt->bindValue(":".$column_name, $value, $param);
}
}
}
$this->stmt->execute();
return $this->stmt->fetchAll(PDO::FETCH_OBJ);
}
catch(PDOException $e) {
error_log(date('Y-m-d h:i:s A') . ":(SQL: " . $this->sql . ")" . $e->getMessage() . "\n", 3, ERROR_PATH . "error.log");
return false;
}
catch(Exception $e) {
error_log(date('Y-m-d h:i:s A') . ":(SQL: " . $this->sql . ")" . $e->getMessage() . "\n", 3, ERROR_PATH . "error.log");
return false;
}
}
}
[Mod note: added [code] tags]