PDO bindValue() adds apostrophes around value

24 views Asked by At

I want to CREATE a table using using PDO prepared statments. The problem is that bindValue adds '' around the binded value. I don't understand why this happens. How can I avoid this so that my table name is set as users and not 'users'? Thanks.

Code:

<?php

$table = "users";

try {

  $db = new PDO("mysql:host=localhost;dbname=test", "root", "");
  $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  $sql = "CREATE TABLE `:table` (id INT(11) AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL)";
  $stmt = $db->prepare($sql);
  $stmt->bindValue(':table', $table);
  $stmt->execute();

} catch(PDOException $e) {

  echo $e->getMessage();

}

?>

Here is the result in phpmyadmin:

enter image description here

0

There are 0 answers