PDO | Select dinâmico
Senhores,
podem me dar uma força???
Quebrando a cabeça e não acho a falha, tenho certeza que será por coisa boba.
Erro:
Fatal error: Call to a member function fetchAll() on a non-object in SERVER\index.php on line 336
Function Select:
class Select {
private $from;
private $where;
private $parameters = array();
private $pdo;
public function __construct(PDO $pdo) {
$this->pdo = $pdo;
}
public function from($from) {
$this->from = (array)$from;
return $this;
}
public function item($item) {
$this->item = (array)$item;
return $this;
}
public function where($where, array $parameters = array()) {
if (empty($this->where)) {
$this->where = sprintf(' WHERE %s', $where);
} else {
$this->where .= sprintf(' AND %s', $where);
}
$this->parameters = array_merge($this->parameters, $parameters);
return $this;
}
public function whereCondicao($whereCondicao) {
$this->whereCondicao = (array)$whereCondicao;
return $this;
}
public function orderby($orderby, array $parameters = array()) {
if (empty($this->orderby)) {
$this->orderby = sprintf(' ORDER BY %s', $orderby);
} else {
$this->orderby .= sprintf(' AND %s', $orderby);
}
$this->parameters = array_merge($this->parameters, $parameters);
return $this;
}
public function execute() {
$stmt = $this->pdo->prepare($this);
foreach ($this->parameters as $key => $value) {
if (is_int($key)) {
$stmt->bindValue(++$key, $value);
} else {
$stmt->bindValue($key, $value);
}
}
$stmt->execute();
return $stmt;
}
public function toString() {
return sprintf('SELECT %s FROM %s%s%s', implode(',', $this->item), implode(',', $this->from), $this->where, $this->orderby);
}
public function __toString() {
return $this->toString();
}
}
Chamada da function:
$select = new Select($conecta);
$select ->item('*')
->from('tbl_sistema')
->where('_status = :_status', array('_status' => '1'))
->orderby('_nome DESC')
->execute();
print_r($stmt->fetchAll(PDO::FETCH_ASSOC));
//echo $select;
Echo no select esta beleza:
SELECT * FROM tbl_sistema WHERE _status = :_status ORDER BY _nome DESC
Mas ao executar print_r, da a falha abaixo:
Fatal error: Call to a member function fetchAll() on a non-object in SERVER\index.php on line 336
É como se eu não estivesse declarando corretamente a varial $stmt... mas não consigo mais "pensar" por conta própria.
Agradeço a ajuda!
Discussão (5)
Carregando comentários...