我正在尝试使用 PHP、PDO 和 MySQL 创建一个表。 为了我的应用程序的需要,表的名称必须是一个变量。
这是我的代码:
$request = $pdo->prepare("CREATE TABLE IF NOT EXISTS :table (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`parent_id` bigint(20) unsigned NOT NULL,
`position` bigint(20) unsigned NOT NULL,
`left` bigint(20) unsigned NOT NULL,
`right` bigint(20) unsigned NOT NULL,
`level` bigint(20) unsigned NOT NULL,
`title` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`type` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`content` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;");
$request->execute(array(
'table'=>$uuid));
我不能在 MySQL 语句中使用“:table”吗?? 目前我写道:
[...]
CREATE TABLE IF NOT EXISTS `$uuid`
[...]
这可行,但对我来说听起来很奇怪 ^^' 这是我问题的唯一解决方案吗?
最佳答案
您不能将表名作为参数传递。如果要使用变量名创建表,则必须使用动态查询。
关于PHP MySQL 创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14444039/