0

I have this class for querying the table in the database.

class QueryBuilder {

    private $pdo;
    public function __construct($pdo) {
        $this->pdo = $pdo;
    }
    public function getAll($table) {
        $stm = $this->pdo->prepare("select * from :targetTable");
        $stm->bindValue(':targetTable', 'albums', PDO::PARAM_STR);
        $stm->execute();
        return $stm->fetchAll();
    }
}

I returns an empty array every time I use bindValue function. If I turn the error reporting mode it states

PDOStatement::execute(): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''albums'' at line 1

When I use simply

  $stm = $this->pdo->prepare("select * from albums");

It works like intended. Function is called by

$query->getAll('albums');

Thanks for help

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
svantetic
  • 300
  • 1
  • 14

0 Answers0