How to use the PDO class in this situation

Asked

Viewed 67 times

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 so I searched with the PDO class 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;
    }
 }

1 answer

0


A certain time I did as follows:

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());
    }
}

On the line return $query->fetchAll(PDO::FETCH_OBJ); between the parentheses I put the kind of feedback I want.

At a glance here has more on 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 full class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.