此查询有效:
$con = mysql_connect("localhost","root","pw");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$sql="INSERT INTO l1_clubmsg (msg, subject, status)
VALUES
(1,1,1)";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
Run Code Online (Sandbox Code Playgroud)
但是当我改变时:
$sql="INSERT INTO l1_clubmsg (msg, subject, status)
VALUES
(1,1,1)";
Run Code Online (Sandbox Code Playgroud)
至:
$sql="INSERT INTO l1_clubmsg (msg, subject, status, to)
VALUES
(1,1,1,1)";
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Error: You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'to) VALUES (1,1,1,1)' at line 1
Run Code Online (Sandbox Code Playgroud)
'to'列确实存在于l1_clubmsg中
任何想法为什么会出错?谢谢
TO 是mysql中的保留字,因此如果要将其用作列名,则需要将其转义为;
INSERT INTO l1_clubmsg (msg, subject, status, `to`) VALUES (1,1,1,1)
Run Code Online (Sandbox Code Playgroud)
逃避所有列和表名称通常是个好主意,因为较新版本的数据库可能有新的保留字,但在这种情况下只需要一个.