Erro No Pdo
seguinte, to fazendo um sistema e ele tá dando um erro na classe do banco de dados, onde eu tento usar o prepare() ele diz que to chamando um método de um não objeto, não sei o que pode ser, enfim, vou deixar as classes:
Model.class.php
<?php
namespace system;
use \system\DB;
Class Model {
private $conn;
protected $table;
public $error;
public function __construct(){
$this->conn = DB::init();
}
public function setTable($table){
$this->table = $table;
}
private function buildInsert(array $data){
$columns = null;
$values = null;
foreach($data AS $key => $value):
$columns .= "{$key},";
$values .= '?,';
endforeach;
$columns = (substr($columns, -1) == ',') ? trim(substr($columns, 0, (strlen($columns) -1))) : $columns;
$values = (substr($values, -1) == ',') ? trim(substr($values, 0, (strlen($values) -1))) : $values;
$query = "INSERT INTO {$this->table}({$columns}) VALUES({$values})";
return trim($query);
}
private function buildUpdate(array $data, array $condition, $operator = '='){
$fields = null;
foreach($data AS $key => $value):
$fields .= "{$key} = ?,";
endforeach;
$where = null;
foreach($condition AS $key => $value):
$where .= "{$key} {$operator} ? AND ";
endforeach;
$fields = (substr($fields, -1) == ',') ? trim(substr($fields, 0, (strlen($fields) -1))) : $fields;
$where = (substr($where, -4) == 'AND ') ? trim(substr($where, 0, (strlen($where) -4))) : $where;
$query = "UPDATE {$this->table} SET {$fields} WHERE {$where}";
return trim($query);
}
private function buildDelete(array $condition, $operator = '='){
$where = null;
foreach($condition AS $key => $value):
$where .= "{$key} {$operator} ? AND ";
endforeach;
$where = (substr($where, -4) == 'AND ') ? trim(substr($where, 0, (strlen($where) -4))) : $where;
$query = "DELETE FROM {$this->table} WHERE {$where}";
return trim($query);
}
public function insert(array $data){
try {
$sql = $this->conn->prepare($this->buildInsert($data));
$count = 1;
foreach($data AS $value):
$type = (is_numeric($value)) ? PDO::PARAM_INT : PDO::PARAM_STR;
$sql->bindValue($count, $value, $type);
$count++;
endforeach;
return $sql->execute();
}catch(PDOException $e){
echo "Erro: {$e->getMessage()}";
}
}
public function update(array $data, array $where){
try {
$sql = $this->conn->prepare($this->buildUpdate($data, $where));
$count = 1;
foreach($data AS $value):
$type = (is_numeric($value)) ? PDO::PARAM_INT : PDO::PARAM_STR;
$sql->bindValue($count, $value, $type);
$count++;
endforeach;
foreach($where AS $value):
$type = (is_numeric($value)) ? PDO::PARAM_INT : PDO::PARAM_STR;
$sql->bindValue($count, $value, $type);
$count++;
endforeach;
return $sql->execute();
}catch(PDOException $e){
echo "Erro: {$e->getMessage()}";
}
}
public function delete(array $where){
try {
$sql = $this->conn->prepare($this->buildDelete($where));
$count = 1;
foreach($where AS $value):
$type = (is_numeric($value)) ? PDO::PARAM_INT : PDO::PARAM_STR;
$sql->bindValue($count, $value, $type);
$count++;
endforeach;
return $sql->execute();
}catch(PDOException $e){
echo "Erro: {$e->getMessage()}";
}
}
public function generic($query, $params = false, $all = true){
try {
$sql = $this->conn->prepare($query);
if($params):
$count = 1;
foreach($params AS $value):
$type = (is_numeric($value)) ? PDO::PARAM_INT : PDO::PARAM_STR;
$sql->bindValue($count, $value, $type);
$count++;
endforeach;
endif;
$sql->execute();
if($all)
return $sql->fetchAll(PDO::FETCH_OBJ);
else
return $sql->fetch(PDO::FETCH_OBJ);
}catch(PDOException $e){
echo "Erro: {$e->getMessage()}";
}
}
}
?>
DB.class.php
<?php
namespace system;
class DB {
private static $conn;
public static function init(){
if(is_null(self::$conn)):
try {
self::$conn = new \PDO(DSN, USER, PASS);
}catch(PDOException $e){
echo "Erro ao conectar com o banco de dados: {$e->getMessage()}";
}
endif;
return self::$conn;
}
}
?>
me ajudem, pfv :s
Discussão (13)
Carregando comentários...