alireza.076
جمعه 04 مرداد 1392, 12:40 عصر
سلام
ببخشید من این ارور رو برای اجرای این فایل میگیرم، دوستان ممنون میشم راهنمایی نمایند:
Fatal error: Call to a member function fetch() on a non-object in /home/admin/domains/nikit.ir/public_html/panel/libraries/Database.php on line 112
خوده فایل هم:
<?php if (__FILE__ == $_SERVER['SCRIPT_FILENAME']) exit('No direct access allowed.');
/**
* Database class
*/
class Database {
/**
* PDO
*
* @access private
*/
private $PDO;
/**
* Config
*
* @access private
*/
private $config;
/**
* Where statements
*
* @access protected
*/
protected $where;
/**
* Constructor
*
* @access public
*/
public function __construct() {
if (!extension_loaded('pdo'))
die('The PDO extension is required.');
$this->config = config_load('database');
$this->connect();
}
/**
* Connect
*
* @access public
*/
public function connect() {
if (empty($this->config['driver']))
die('Please set a valid database driver from database.php');
$driver = strtoupper($this->config['driver']);
switch ($driver) {
case 'MYSQL':
try {
$this->PDO = new PDO('mysql:host=' . $this->config['hostname'] . ';dbname=' . $this->config['dbname'], $this->config['username'], $this->config['password']);
$this->PDO->query('SET NAMES ' . $this->config['char_set']);
} catch (PDOException $exception) {
die($exception->getMessage());
}
return $this->PDO;
break;
default:
die('This database driver does not support: ' . $this->config['driver']);
}
}
/**
* Executes an sql statement
*
* @access public
*/
public function query($statement) {
return $this->PDO->query($statement);
}
/**
* Returns the number of rows affected
*
* @access public
*/
public function row_count($statement) {
return $this->PDO->query($statement)->rowCount();
}
/**
* Execute query and return one row in assoc array
*
* @access public
*/
public function fetch_row_assoc($statement) {
return $this->PDO->query($statement)->fetch(PDO::FETCH_ASSOC);
}
/**
* Returns the id of the last inserted row
*
* @access public
*/
public function last_insert_id() {
return $this->PDO->lastInsertId();
}
/**
* Builds the where statements to a sql query
*
* @access public
*/
public function where($value) {
$this->where = $value;
return $this;
}
/**
* Insert a value into a table
*
* @access public
*/
public function insert($table, $values) {
foreach ($values as $key => $value)
$field_names[] = $key . ' = :' . $key;
$sql = "INSERT INTO " . $table . " SET " . implode(', ', $field_names);
$stmt = $this->PDO->prepare($sql);
foreach ($values as $key => $value)
$stmt->bindValue(':' . $key, $value);
$stmt->execute();
}
/**
* Update a value in a table
*
* @access public
*/
public function update($table, $values) {
foreach ($values as $key => $value)
$field_names[] = $key . ' = :' . $key;
$sql = "UPDATE " . $table . " SET " . implode(', ', $field_names) . " ";
$counter = 0;
if($this->where){
foreach ($this->where as $key => $value) {
if ($counter == 0) {
$sql .= "WHERE {$key} = :{$key} ";
} else {
$sql .= "AND {$key} = :{$key} ";
}
$counter++;
}}
$stmt = $this->PDO->prepare($sql);
foreach ($values as $key => $value)
$stmt->bindValue(':' . $key, $value);
if($this->where){
foreach ($this->where as $key => $value)
$stmt->bindValue(':' . $key, $value);
}
$stmt->execute();
}
/**
* Delete a record
*
* @access public
*/
public function delete($table) {
$sql = "DELETE FROM " . $table . " ";
$counter = 0;
foreach ($this->where as $key => $value) {
if ($counter == 0) {
$sql .= "WHERE {$key} = :{$key} ";
} else {
$sql .= "AND {$key} = :{$key} ";
}
$counter++;
}
$stmt = $this->PDO->prepare($sql);
foreach ($this->where as $key => $value)
$stmt->bindValue(':' . $key, $value);
$stmt->execute();
}
public function errorCode(){
return $this->PDO->errorcode();
}
}
?>
ببخشید من این ارور رو برای اجرای این فایل میگیرم، دوستان ممنون میشم راهنمایی نمایند:
Fatal error: Call to a member function fetch() on a non-object in /home/admin/domains/nikit.ir/public_html/panel/libraries/Database.php on line 112
خوده فایل هم:
<?php if (__FILE__ == $_SERVER['SCRIPT_FILENAME']) exit('No direct access allowed.');
/**
* Database class
*/
class Database {
/**
* PDO
*
* @access private
*/
private $PDO;
/**
* Config
*
* @access private
*/
private $config;
/**
* Where statements
*
* @access protected
*/
protected $where;
/**
* Constructor
*
* @access public
*/
public function __construct() {
if (!extension_loaded('pdo'))
die('The PDO extension is required.');
$this->config = config_load('database');
$this->connect();
}
/**
* Connect
*
* @access public
*/
public function connect() {
if (empty($this->config['driver']))
die('Please set a valid database driver from database.php');
$driver = strtoupper($this->config['driver']);
switch ($driver) {
case 'MYSQL':
try {
$this->PDO = new PDO('mysql:host=' . $this->config['hostname'] . ';dbname=' . $this->config['dbname'], $this->config['username'], $this->config['password']);
$this->PDO->query('SET NAMES ' . $this->config['char_set']);
} catch (PDOException $exception) {
die($exception->getMessage());
}
return $this->PDO;
break;
default:
die('This database driver does not support: ' . $this->config['driver']);
}
}
/**
* Executes an sql statement
*
* @access public
*/
public function query($statement) {
return $this->PDO->query($statement);
}
/**
* Returns the number of rows affected
*
* @access public
*/
public function row_count($statement) {
return $this->PDO->query($statement)->rowCount();
}
/**
* Execute query and return one row in assoc array
*
* @access public
*/
public function fetch_row_assoc($statement) {
return $this->PDO->query($statement)->fetch(PDO::FETCH_ASSOC);
}
/**
* Returns the id of the last inserted row
*
* @access public
*/
public function last_insert_id() {
return $this->PDO->lastInsertId();
}
/**
* Builds the where statements to a sql query
*
* @access public
*/
public function where($value) {
$this->where = $value;
return $this;
}
/**
* Insert a value into a table
*
* @access public
*/
public function insert($table, $values) {
foreach ($values as $key => $value)
$field_names[] = $key . ' = :' . $key;
$sql = "INSERT INTO " . $table . " SET " . implode(', ', $field_names);
$stmt = $this->PDO->prepare($sql);
foreach ($values as $key => $value)
$stmt->bindValue(':' . $key, $value);
$stmt->execute();
}
/**
* Update a value in a table
*
* @access public
*/
public function update($table, $values) {
foreach ($values as $key => $value)
$field_names[] = $key . ' = :' . $key;
$sql = "UPDATE " . $table . " SET " . implode(', ', $field_names) . " ";
$counter = 0;
if($this->where){
foreach ($this->where as $key => $value) {
if ($counter == 0) {
$sql .= "WHERE {$key} = :{$key} ";
} else {
$sql .= "AND {$key} = :{$key} ";
}
$counter++;
}}
$stmt = $this->PDO->prepare($sql);
foreach ($values as $key => $value)
$stmt->bindValue(':' . $key, $value);
if($this->where){
foreach ($this->where as $key => $value)
$stmt->bindValue(':' . $key, $value);
}
$stmt->execute();
}
/**
* Delete a record
*
* @access public
*/
public function delete($table) {
$sql = "DELETE FROM " . $table . " ";
$counter = 0;
foreach ($this->where as $key => $value) {
if ($counter == 0) {
$sql .= "WHERE {$key} = :{$key} ";
} else {
$sql .= "AND {$key} = :{$key} ";
}
$counter++;
}
$stmt = $this->PDO->prepare($sql);
foreach ($this->where as $key => $value)
$stmt->bindValue(':' . $key, $value);
$stmt->execute();
}
public function errorCode(){
return $this->PDO->errorcode();
}
}
?>