Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Erilton

PDO | Select dinâmico

Recommended Posts

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!

Compartilhar este post


Link para o post
Compartilhar em outros sites

De onde vem essa variável [inline]$stmt[/inline]?

 

Olá Henrique... vem da função execute()..

 

O pdo, ao meu ver, está instanciado corretamente:

 

$pdo = new PDO($conexao,USER,PASS);

Compartilhar este post


Link para o post
Compartilhar em outros sites

Acho que isso resolve:

 

$stmt = $select	->item('*')
			->from('tbl_sistema')
			->where('_status = :_status', array('_status' => '1')) 
			->orderby('_nome DESC')
			->execute();

Compartilhar este post


Link para o post
Compartilhar em outros sites

Acho que isso resolve:

 

$stmt = $select	->item('*')
			->from('tbl_sistema')
			->where('_status = :_status', array('_status' => '1')) 
			->orderby('_nome DESC')
			->execute();

 

Matou a pau Henrique!

Muito obrigado!

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.