我写过这个mysql qyery -
$creat = "CREATE TABLE $out(rollno varchar(10) PRIMARY KEY, bracode varchar(10),cgpi varchar(5),priority int(2))";
if (mysqli_query($connection, $creat)) {
echo "<center>Elective successfully published</center><br>";
} else {
echo "Error: " . $creat . "<br>" . mysqli_error($connection);
}
此$out
中的是值为csf-313的变量 并抛出这个错误 -
错误:CREATE TABLE csd-315(rollno varchar(10)PRIMARY KEY,bracode varchar(10),cgpi varchar(5),priority int(2)) 您的SQL语法有错误;查看与您的MariaDB服务器版本对应的手册,以便在&#39; -315附近使用正确的语法(rollno varchar(10)PRIMARY KEY,bracode varchar(10),cgpi varchar(5),priorit&#39; at line 1
答案 0 :(得分:2)
尝试在表名上使用反向标记,这样就可以转义表名。
$creat = "CREATE TABLE `$out`(`rollno` varchar(10) PRIMARY KEY, `bracode` varchar(10),`cgpi` varchar(5),`priority` int(2))";
if (mysqli_query($connection, $creat)) {
echo "<center>Elective successfully published</center><br>";
} else {
echo "Error: " . $creat . "<br>" . mysqli_error($connection);
}
你必须读取的内容是MySQL Schema Object Names,指定
标识符引号字符是反引号(`)
还指定了
如果启用了ANSI_QUOTES SQL模式,也允许使用 在双引号内引用标识符
在这种情况下你可以写......
$creat = 'CREATE TABLE "$out"("rollno" varchar(10) PRIMARY KEY, "bracode" varchar(10),"cgpi" varchar(5),"priority" int(2))';
if (mysqli_query($connection, $creat)) {
echo "<center>Elective successfully published</center><br>";
} else {
echo "Error: " . $creat . "<br>" . mysqli_error($connection);
}
答案 1 :(得分:0)
尝试ANSI_QUOTES
SET SQL_MODE = ANSI_QUOTES;