How to use the class PDO in this situation

4

I am using the PDO class to connect the database but in this particular situation I do not know how to use the PDO because in the mysql_fetch parameter I need to put a variable and for what I researched with the class PDO it is not the same way.

public function retornaDados($tipo=NULL){
    switch(strtolower($tipo)){
           case "array":
           return mysql_fetch_array();
           break;
           case "assoc";
           return  mysql_fetch_assoc();
           break;
           case "object":
           return mysql_fetch_object();
           break;
           default:
           return mysql_fetch_object()
           break;
    }
 }
    
asked by anonymous 25.11.2014 / 15:57

1 answer

0

At one point I did the following:

public static function selectAll($tabela, $condicao = NULL){
    $sql = 'SELECT * FROM '.$tabela;
    if(!is_null($condicao)) $sql .= ' WHERE '.$condicao;
    try
    {
        $query = self::$conexao->prepare($sql);
        $query->execute();
        return $query->fetchAll(PDO::FETCH_OBJ);
    }
    catch(PDOException $e)
    {
        self::erroLog(__FILE__, __FUNCTION__, $e->getMessage());
    }
}

In the return $query->fetchAll(PDO::FETCH_OBJ); line between parentheses I put the return type I want.

At a glance here has more about the parameters. In case the queries always returned as the specified parameter, in my case I will always have a return in the form of PDO::FETCH_OBJ .

Take a look at the GitHub has the complete class.

    
25.11.2014 / 16:16