Fatal error error: Uncaught Error: Call to undefined function getList ()

1

I need to reuse this function so that it uses fetchAll but also use num_rows, the problem is that if I do return fetchAll or num_rows it works, but in that case I would have to create two functions just for one to use fetch and another rows, because if I command to return the execute and I try to use the fetch the nuws in this way of the error: getList () -> fetchAll (); I want to use it like this:

public function getList($table = "produto",$orderTable = "id",$order = "ASC",$limited = 1){
        $this->list = parent::Conecta()->prepare("SELECT * FROM $table ORDER BY $orderTable $order LIMIT :limited");
        $this->list->bindValue(":limited",$limited,PDO::PARAM_INT);
        return $this->list->execute();

getList () -> num_rows or getList () -> fetchAll () using only a single function, but gives error, only works if I create a function for each example:

public function getList($table = "produto",$orderTable = "id",$order = "ASC",$limited = 1){
        $this->list = parent::Conecta()->prepare("SELECT * FROM $table ORDER BY $orderTable $order LIMIT :limited");
        $this->list->bindValue(":limited",$limited,PDO::PARAM_INT);
        $this->list->execute();
        return $this->list->fetchAll();
    
asked by anonymous 12.03.2017 / 22:23

1 answer

3

The error has nothing to do with PDO or mysql, the problem was how you called your method getList

  

Fatal error: Uncaught Error: Call to undefined function getList ()

For methods (functions) of classes you need -> or :: (maybe static)

If it is within the class itself use $this :

$this->getList();

If it is in the object:

$meuobj = new NomeDaSuaClasse;
$meuobj->getList();

And instead of returning execute() just return $this->list and it will be possible to use get num_row and featchAll

public function getList($table = "produto",$orderTable = "id",$order = "ASC",$limited = 1) {
    $this->list = parent::Conecta()->prepare("SELECT * FROM $table ORDER BY $orderTable $order LIMIT :limited");
    $this->list->bindValue(":limited",$limited,PDO::PARAM_INT);
    $this->list->execute();

    return $this->list;
}

Otherwise there is no num_rows in PDO, what exists is rowCount : link , then the call would look like this:

$meuobj = new NomeDaSuaClasse;

$x = $meuobj->getList();

$x->rowCount(); //Conta linhas

$x->fetchAll(); //Pega os resultados

"I recommend" to follow the link documentation, make up your own head or "kick" until you get it right, follow the example of the documentation learn and understand what each thing is in the "object."

    
12.03.2017 / 23:12