php MySQL 语法错误;“检查与您的 MySQL 服务器版本相对应的手册..”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2352336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:08:51  来源:igfitidea点击:

MySQL Syntax Error; "check the manual that corresponds to your MySQL server version.."

phpsqlmysqlmysql-error-1064

提问by Lawrence

I've been looking all over the internet for a solution to the following 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 'primary, username, password, password2) VALUES (null, 'hello', 'hello', 'hello')' at line 1"

I have no idea what is going on.. I know you will ask what my code is so here:

我不知道发生了什么..我知道你会问我的代码是什么:

$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);

I've been trying to figure it out for around 4/5 hours now and have had no success.
Thanks,
Lawrence

我一直试图弄清楚现在大约 4/5 个小时,但没有成功。
谢谢,
劳伦斯

回答by Pascal MARTIN

primaryis a reserved keyword, in SQL, which means that you should either :

primary保留关键字,在 SQL 中,这意味着您应该:

  • rename that column -- would be a good idea, to avoid that kind od situation
  • or use backticks arround that name
  • 重命名该列 - 是个好主意,以避免这种情况
  • 或在该名称周围使用反引号

Here what the query would look like in the second case :

下面是第二种情况下的查询:

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


Note : and you should escape your values, using mysql_real_escape_string, to avoid SQL Injections!


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

回答by Stephano

Try not to name your tables or columns with relitively common names like primary and details.

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

While they may not be reserved words in the flavor of SQL you are currently using, you never know when you might be supporting other types (Postgres, Oracle, etc.).

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

You can also use this handy-dandy reserved word checker.

你也可以使用这个方便的保留字检查器

Followup Question:
I would like to know who wrote the error statement you are getting, which essentially says RTM? Hilarious. I'm going to use that in my next try catch. :)

后续问题:
我想知道您收到的错误声明是谁写的,基本上是说RTM?好笑。我将在下一次 try catch 中使用它。:)

回答by Robert Kluin

Primary is a reserved word. What is the table definition?

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

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

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

回答by duffymo

I'd rename that first column to something else: "primary" is a reserved word in MySQL:

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

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

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