[英]What's wrong with my MySQL syntax?
I've followed the syntax guide for creating tables exactly how it should be but I keep getting a syntax error. 我一直遵循语法指南来创建表的确切格式,但是我不断收到语法错误。 Can someone help me out?
有人可以帮我吗?
$createQuery = sprintf("CREATE TABLE %s(message_ID INT NOT NULL AUTO_INCREMENT, message TEXT, sender TEXT, recipient TEXT, PRIMARY KEY( message_ID )) ENGINE=MyISAM;",$tablename);
$createResult = mysqli_query($connect, $createQuery);
$tablename in this case is 1-3 $ tablename在这种情况下是1-3
您需要像这样转义表名
$tablename = '`1-3`';
Try adding back ticks to the %s
: 尝试将刻度添加回
%s
:
$createQuery = sprintf("CREATE TABLE `%s` (message_ID INT NOT NULL AUTO_INCREMENT, message TEXT, sender TEXT, recipient TEXT, PRIMARY KEY( message_ID )) ENGINE=MyISAM;",$tablename);
$createResult = mysqli_query($connect, $createQuery);
Also your original code had the %s
right next to the parenthesis (
. Technically with back ticks, that should not be an issue, but you should get in the habit of just adding spaces between things when dealing with MySQL queries. 同样,您的原始代码在圆括号
(
。)旁边有%s
。从技术上讲,这不是问题,但是您应该养成在处理MySQL查询时在事物之间添加空格的习惯。
That said, you could also achieve the same results without sprintf()
by just placing $tablename
directly into the code because double-quotes allow for string substitution: 就是说,通过直接将
$tablename
放入代码中,即使不使用sprintf()
也可以实现相同的结果,因为双引号允许字符串替换:
$createQuery = "CREATE TABLE `$tablename` (message_ID INT NOT NULL AUTO_INCREMENT, message TEXT, sender TEXT, recipient TEXT, PRIMARY KEY( message_ID )) ENGINE=MyISAM;";
$createResult = mysqli_query($connect, $createQuery);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.