2

I'm trying to create database like this:

const QUERY_DB_CREATE = "
    CREATE DATABASE :db_name
";
if (database is not exists) {
    $stmt = $conn->prepare(self::QUERY_DB_CREATE);
    $stmt->bindValue(':db_name', $db_name, 'string');

    return $stmt->execute();
}

Error

PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near "'testtesttest2'"

What am I doing wrong?

  • I suspect many things are wrong. To start with, you can't use parameters for table and column names in prepared statements. – Cerad Sep 07 '15 at 17:43

1 Answers1

2

You should take a look at the DoctrineBundle command (for use in the Symfony framework) for creating databases. Without knowing more about your implementation details, these lines especially should be useful in your case:

    $tmpConnection = DriverManager::getConnection($params);
    ...
    $tmpConnection->getSchemaManager()->createDatabase($name);
iisisrael
  • 583
  • 6
  • 6