0

this is already asked million times already, but I got this error while creating a database in php using pdo. Below is the part that causes this error.

try {
    $conn = new PDO("mysql:host=$servername", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // create database 
    $db_query = $conn->prepare("CREATE DATABASE IF NOT EXISTS ?");
    $db_query->execute([$myDB]);
} catch(PDOException $e) {
    echo  "<br>" . $e->getMessage();
}

I could create the database just fine without using prepare, so I wonder if prepare cannot be used to create database by design.

samayo
  • 16,163
  • 12
  • 91
  • 106
  • Why are you putting spaces between your code as ` -> `? This is invalid – samayo May 20 '17 at 22:55
  • a `?` placeholder cannot be used for a database/column/table identifier. They are only appropriate for values. You should instead validate the `$myDB` string against something like a regular expression, assuming you have strict limits on how the database should be named. – Michael Berkowski May 20 '17 at 23:30

0 Answers0