我想创建一个mysql表,其所有字段均为php变量。为什么执行此代码时出错?
$stmttt = $pdo->query("
CREATE TABLE $test (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
$name VARCHAR(60) NOT NULL,
$tel VARCHAR(30) NOT NULL,
$email VARCHAR(50) NOT NULL,
$city VARCHAR(50),
CMSdate TIMESTAMP)
");
答案 0 :(得分:0)
您可以使用{}
正确地转义变量。尝试
$stmttt = $pdo->query("
CREATE TABLE ${test} (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
${name} VARCHAR(60) NOT NULL,
${tel} VARCHAR(30) NOT NULL,
${email} VARCHAR(50) NOT NULL,
${city} VARCHAR(50),
CMSdate TIMESTAMP)
");
还可以查看一个有效的示例here