Fatal Error: Uncaught Error: Call to Undefined Function getList()

Asked

Viewed 842 times

1

I need to reuse this function so that it uses fetchAll but also use num_rows, the problem is that if I return fetchAll or num_rows it works, but in that case I would have to create two functions just to use fetch and the other Rows, because if I return the execute and try to use fetch the nuws this way from the error: getList()->fetchAll(); I want to wear 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();
  • what kind of mistake?

  • It says that the getList method is undefined, see: Fatal error: Uncaught Error: Call to Undefined Function getList() in

1 answer

4


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 the -> or :: (chance it is static)

If you are inside the class itself use the $this:

$this->getList();

If it’s in the object:

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

And instead of returning the execute() just return the $this->list and it will be possible to use catch the 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;
}

Outside that there is no num_rows in PDO, what exists is rowCount: http://php.net/manual/en/pdostatement.rowcount.php, then the call would go like this:

$meuobj = new NomeDaSuaClasse;

$x = $meuobj->getList();

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

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

"Recommend" follow the examples in the documentation http://php.net/manual/en/pdostatement.execute.php, do not invent your own head or stay "kicking" until you get it right, follow the example of the documentation learn and understand what is each thing in the "object".

  • but theoretically I using "Return $this->list->execute();" or "Return $this->list" should be the same thing, in both cases I’m having the run returned, or it’s not true?

  • besides even doing the way you said returns the same error

  • @Otaviofagundes posted the wrong links, the correct is the PDO, I’ll hit here

  • @Otaviofagundes execute only returns true or false see http://php.net/manual/en/pdostatement.execute.php

  • Now it’s worked out, thank you!

  • @Otaviofagundes added some details about num_rows

  • fetchAll is working perfectly, just the num_rows that is giving me this error: Fatal error: Uncaught Error: Call to Undefined method Pdostatement::num_rows() in.... I used mysqli and in it num_rows worked, I think the PDO should be different

  • 1

    @Otaviofagundes was like I said, you have to follow the documentation, num_rows is in mysqli, in PDO we used rowCount: http://php.net/manual/en/class.pdostatement.php (even was my previous comment already quoted this detail in the reply)

  • I just saw your new lines on the num_rows and it worked, thank you very much, sorry for the work ^^

Show 4 more comments

Browser other questions tagged

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