【问题标题】:MySQL Syntax Error; "check the manual that corresponds to your MySQL server version.." [duplicate]MySQL 语法错误; “检查与您的 MySQL 服务器版本相对应的手册..” [重复]
【发布时间】:2010-02-28 19:34:37
【问题描述】:

我一直在互联网上寻找解决以下错误的方法;

"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 'primary, username, password, password2) VALUES (null, 'hello', 'hello', 'hello')' at line 1"

我不知道发生了什么。我知道你会在这里问我的代码是什么:

$con = mysql_connect("localhost","root","*****");
    if (!$con)
      {
      die('Server overload, please try again' . mysql_error());
      }

    mysql_select_db("users", $con);

    $sql = "INSERT INTO details (primary, username, password, password2) VALUES (null, '$_POST[username]', '$_POST[password]', '$_POST[password2]')";

    if (!mysql_query($sql,$con))
      {
      die('Error: Server overload, try again' . mysql_error());
      }
    echo "You have signed up successfully!";

    mysql_close($con);

我已经尝试了大约 4/5 小时,但没有成功。
谢谢,
劳伦斯

【问题讨论】:

  • 我刚刚搜索了“您的 SQL 语法有错误;请查看手册”。也许您只是在谷歌上搜索整个错误?前几个结果是正确答案。

标签: php sql mysql mysql-error-1064


【解决方案1】:

primary 在 SQL 中是 reserved keyword,这意味着您应该:

  • 重命名该列 -- 这是一个好主意,以避免这种奇怪的情况
  • 或在该名称周围使用反引号

这里查询在第二种情况下的样子:

INSERT INTO details (`primary`, `username`, `password`, `password2`)
VALUES (null, 'hello', 'hello', 'hello')


注意:您应该使用mysql_real_escape_string 转义您的值,以避免SQL Injections

【讨论】:

  • 谢谢 :) 我认为这将是一个简单的错误!全部整理完毕。
  • 不客气 :-) ;;不要忘记“逃避你的价值观”部分!
【解决方案2】:

尽量不要使用相对常见的名称(如主要名称和详细信息)来命名您的表或列。

虽然它们可能不是您当前使用的 SQL 风格的保留字,但您永远不知道何时可能支持其他类型(Postgres、Oracle 等)。

你也可以使用这个方便的花花公子reserved word checker

后续问题:
我想知道你得到的错误声明是谁写的,它本质上是 RTM?好笑。我将在下一次尝试中使用它。 :)

【讨论】:

    【解决方案3】:

    Primary 是保留字。表定义是什么?

    http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html

    【讨论】:

      【解决方案4】:

      我会将第一列重命名为其他名称:“primary”是 MySQL 中的保留字:

      http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-27
        • 1970-01-01
        • 1970-01-01
        • 2014-03-14
        • 1970-01-01
        • 1970-01-01
        • 2019-11-22
        • 1970-01-01
        相关资源
        最近更新 更多