Passing parameter inside the method in the foreach

Asked

Viewed 163 times

0

I have the index that contains my class call CRUD and the foreach calling the class method crud. But within the foreach I’m trying to pass the table name parameter, but it shows nothing. If I take the parameter and put the table name inside the method, it works well. I don’t know what I do! =/

Index:

<?php
     require_once('classes/crud.class.php');
     $mostrarpar = new crud();

?>
<?php foreach ($mostrarpar->Listar('moedas_par') as $res) { ?>
     <li><?php echo $res['par_nome']; ?></li>
<?php } ?>

CRUD:

public function Listar($table) {
    $this->query = $this->setConn()->prepare("SELECT * FROM'.$table.'");
    $this->query->execute();
    return $this->query->fetchall(PDO::FETCH_ASSOC);
}
  • Have you tried it print_r($res) or var_dump($res) to see if that answer has what you want?

  • 2

    Try it like this $this->query = $this->setConn()->prepare("SELECT * FROM ".$table); , was missing a space between the table name and the FROM, and the concatenation was wrong

  • Putz, what a mistake! hehe, thank you Augusto. Fixed and working perfectly. ;)

No answers

Browser other questions tagged

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