-1

Please help me, I can't using bindValue() in PDOStatement.

        $statement = self::$dbConn->prepare("SELECT  * FROM :tableDB WHERE id = :id");
        $statement->bindValue(":id", $id, PDO::PARAM_INT);
        $statement->bindValue(":tableDB", $tableDB, PDO::PARAM_STR);
        $statement->execute();
        $statement->setFetchMode(PDO::FETCH_ASSOC);
        $result = $statement->fetchAll();

When i run this script.

        $statement = self::$dbConn->prepare("SELECT  * FROM :tableDB WHERE id = :id");
        $statement->bindValue(":id", $id, PDO::PARAM_INT); // Return true
        $statement->bindValue(":tableDB", $tableDB, PDO::PARAM_STR); // Return true

But when run to:

$statement->execute(); // Return false.
Mad Dog Tannen
  • 7,129
  • 5
  • 31
  • 55

1 Answers1

1

You're binding table name. You cannot do that.

Use the table name directly in the query as follows:

$statement = self::$dbConn->prepare("SELECT  * FROM table_name WHERE id = :id");
$statement->bindValue(":id", $id, PDO::PARAM_INT);
// $statement->bindValue(":tableDB", $tableDB, PDO::PARAM_STR); <-- Remove this line

Update: Replaced query to use table name instead of variable.

vee
  • 38,255
  • 7
  • 74
  • 78