How to return the result of a select with prepared statement?

0

People, I'm not able to return the result of a search with prepared statement.

Here is the code:

public function search($obj) {
        $connection = new Connection();
        $this->db = $connection->conectar();

        /* String SQL de buscar todos os registros */
        $stringSQL = "SELECT * FROM fornecedor WHERE cnpj = :cnpj";

        $statement = $this->db->prepare($stringSQL);

        /* parâmetros do prepared statement */
        $statement->bindparam(":cnpj", $obj->getCnpj());

        /* executa a query */
        $statement->execute();

        $result = $statement->get_result();

        unset($this->db);
        return $result;
    }       

I have the following error:

  

Fatal error: Call to undefined method PDOStatement :: get_result () in C: \ xampp \ htdocs \ inove \ peoplelella-master \ production \ Classes \ Persistence \ ProviderCrud.php on line 123

Any help?

Thank you

    
asked by anonymous 30.07.2017 / 18:08

1 answer

3
public function search($obj) {
    $connection = new Connection();
    $this->db = $connection->conectar();

    /* String SQL de buscar todos os registros */
    $stringSQL = "SELECT * FROM fornecedor WHERE cnpj = :cnpj";

    $statement = $this->db->prepare($stringSQL);

    /* parâmetros do prepared statement */
    $statement->bindparam(":cnpj", $obj->getCnpj());

    /* executa a query */
    $statement->execute();

    /* retorna o resultado */
    $statement->fetchAll(\PDO::FETCH_ASSOC);
    return $statement;
}       

$ stmt-> fetchAll (\ PDO :: FETCH_ASSOC); // This line returns all results found in execute () .. if you want to return a single result use $ stmt-> fetch (\ PDO :: FETCH_ASSOC); I did not try it anymore, I think it's working .. Good luck!

    
30.07.2017 / 18:21