Mysql查询无法使用PHP并抛出CREATE TABLE错误

时间:2017-03-05 13:55:43

标签: php mysql mysqli

我写过这个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

2 个答案:

答案 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;

SQL ANSI_QUOTES

  

您必须阅读Database, Table and Column Naming Conventions?