PHP Dynamic Function

0

How do I override this function to dynamically make a select on more than one table?

  public function Lista(){
        $results = array();
        $stmt = $this->conn->prepare("SELECT * FROM 'tabela'");
        $stmt->execute();
            if($stmt) {
                while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
                    $not = new Not();
                    $not->setid($row->ID);
                    $not->setimg1($row->Img1);
                    $not->setimg2($row->Img2);
                    $not->setimg3($row->Img3);
                    $results[] = $not;
                }
            }
        return $results;
    }

Because I need multiple select's in multiple tables, I have not found a simple way, except to do a function for each select. The same is repeated for update and insert . Any tips?

    
asked by anonymous 30.08.2016 / 02:26

1 answer

2

I think you want something like this.

 public function Lista($tabela){
        $sql = "SELECT * FROM '$tabela'";
        $results = array();
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
            if($stmt) {
                while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
                    $not = new Not();
                    $not->setid($row->ID);
                    $not->setimg1($row->Img1);
                    $not->setimg2($row->Img2);
                    $not->setimg3($row->Img3);
                    $results[] = $not;
                }
            }
        return $results;
    }

To pass the value of the table, just write its name when calling the function List () as in the example below:

Lista('usuario');

So the function returns data from the user table

    
30.08.2016 / 03:35