PDA

View Full Version : مبتدی: ارور در یک فایل



alireza.076
جمعه 04 مرداد 1392, 11: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();
}

}

?>

Tarragon
جمعه 04 مرداد 1392, 23:27 عصر
سلام
دوستان عزیز منم همین مشکل رو تقریبا دارم.
کسی می تونه کمک کنه؟

Tarragon
شنبه 05 مرداد 1392, 11:48 صبح
up.............................................

wallfa
شنبه 05 مرداد 1392, 12:51 عصر
عزیز اول بررسی کن ببین اسکیوال استیت منتت false نیست و بعد اگر نبود فیچش کن !

wallfa
شنبه 05 مرداد 1392, 12:54 عصر
function fetch_row_assoc($statement) {

if($statement === false){
echo $this->pdo->errorInfo();
}
/// injoor mishe fahmid error az kojast !
return $this->PDO->query($statement)->fetch(PDO::FETCH_ASSOC);

}

Tarragon
شنبه 05 مرداد 1392, 13:18 عصر
function fetch_row_assoc($statement) {

if($statement === false){
echo $this->pdo->errorInfo();
}
/// injoor mishe fahmid error az kojast !
return $this->PDO->query($statement)->fetch(PDO::FETCH_ASSOC);

}



سلام
مطمئنید؟
آخه statment دستور qeury هستش.

Tarragon
شنبه 05 مرداد 1392, 13:34 عصر
الان این چجوری حلش کنم؟
فکر کنم مربوط به static و این قضایا باشه /

Fatal error: Call to a member function fetch() on a non-object

Tarragon
شنبه 05 مرداد 1392, 21:29 عصر
up........................