I have been spending 1hour+ looking at the code and I can't figure out why it doesn't work and am seeking advice rather than ripping my hair off.
I am just trying to write a php script to create some tables in MySQL.
The below code doesn't create any table at all. Why is it?
//prepared statement to execute for table creation
$query = "CREATE TABLE IF NOT EXISTS :table (:columns)";
$stmt = $dbh->prepare($query);
$stmt->bindParam(":table", $table, PDO::PARAM_STR);
$stmt->bindParam(":columns", $columns, PDO::PARAM_STR);
//creating the members table
$table = "members";
$columns = "user VARCHAR(20), password VARCHAR(20), INDEX(user(5))";
try {
$stmt->execute();
} catch (Exception $ex) {
echo "Exception: ".$ex->getMessage(); }
/*
$sql = "CREATE TABLE IF NOT EXISTS $table ($columns)";
$dbh->query($sql);*/
echo "Table $table now in database ". DB_NAME. "<br>";
When I comment out the prepared stmt and use a straight query, it works and table created, so there is nothing wrong with $dbh.
//prepared statement to execute for table creation
/*
$query = "CREATE TABLE IF NOT EXISTS :table (:columns)";
$stmt = $dbh->prepare($query);
$stmt->bindParam(":table", $table, PDO::PARAM_STR);
$stmt->bindParam(":columns", $columns, PDO::PARAM_STR);
*/
//creating the members table
$table = "members";
$columns = "user VARCHAR(20), password VARCHAR(20), INDEX(user(5))";
/*
try {
$stmt->execute();
} catch (Exception $ex) {
echo "Exception: ".$ex->getMessage(); }
*/
$sql = "CREATE TABLE IF NOT EXISTS $table ($columns)";
$dbh->query($sql);
echo "Table $table now in database ". DB_NAME. "<br>";