Does the Pdostatement bindValue() not work with the table name?

Asked

Viewed 90 times

0

Well, I have this code:

public function Ler($tabela="funcionarios"){
        $this->sql = $this->pdo->prepare("SELECT * FROM :tabela");
        $this->sql->bindValue(':tabela',$tabela);
        $this->sql->execute();
        return $this->sql->fetchAll();
    }

Then it returns an empty array.

But Already this code:

public function Ler(){
        $this->sql = $this->pdo->prepare("SELECT * FROM funcionarios");
        $this->sql->execute();
        return $this->sql->fetchAll();
    }

returns the array with the records, because bindValue does not work?

From now on, thank you :)

1 answer

0

No, the substitution of values with bindValue does not work with all parts of the command, such as the command itself (select, update, etc.), nor with field or table names, works with values in the values, where, etc..

This needs to be done before, for example:

$sql = "SELECT * FROM $tabela";
$this->sql = $this->pdo->prepare($sql);

Browser other questions tagged

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