【问题标题】:You have an error in your SQL syntax; check the manual for the right syntax to use near 'AND `user_id` = 1'您的 SQL 语法有错误;检查手册以了解在 'AND `user_id` = 1' 附近使用的正确语法
【发布时间】:2014-04-16 19:24:55
【问题描述】:

我知道有很多这样的问题,但它们似乎都不起作用,我在语法上找不到错误!

mysql_query("SELECT `type` FROM `poststuff` WHERE `post_id` = 7 AND `user_id` = 1");

任何帮助都会很棒! (顺便说一句,标题上有错误) 完整代码(给你们的人-,-)

function update_post($post_id, $user_id) {
$result = mysql_result(mysql_query("SELECT `type` FROM `poststuff` WHERE `post_id` = $post_id AND `user_id` = $user_id"), 0) or die(mysql_error());
if ($result !== null) {
if ($result === "no" ) {
$actual_count = mysql_result(mysql_query("SELECT `$result` FROM `posts` WHERE `post_id` = $post_id"), 0);
mysql_query("UPDATE `posts` SET `$actual_count` = $actual_count+1 WHERE `post_id` = $post_id AND `type` = 'no'");
}

【问题讨论】:

  • 给出你的表结构
  • 7 和 1 实际上在查询中还是变量?
  • 是的,它们不需要单引号,因为它们是整数,对吧?
  • 是的,整数不需要单引号。
  • 我在 phpMyAdmin 中遇到了这个错误,只有这个查询!

标签: php mysql sql syntax


【解决方案1】:

您发布的 SQL 文本中似乎没有问题。我建议您验证它是否是 AND 关键字前面的有效空白字符。

根据错误消息,我怀疑查询中 AND 关键字前面有一个无效字符。

如果您运行的查询在 AND 关键字之前有 逗号 或其他一些非空白字符,则会出现这种错误。

例如,运行这个查询:

SELECT `type` FROM `poststuff` WHERE `post_id` = 7,AND `user_id` = 1
                                                  ^

会导致像你得到的错误:

... error in your SQL syntax; ... near 'AND `user_id` = 1'

如果等号后没有值,而只有空格,则会报告相同的错误。例如,以下查询将返回相同的错误:

SELECT `type` FROM `poststuff` WHERE `post_id` = AND `user_id` = 1
                                                ^

【讨论】:

  • 我也想过这个,但是当我将上面的内容复制/粘贴到我的查询浏览器中时,我没有收到那个错误。也许 OP 没有将他的查询复制粘贴到 SO,而是重新输入?
  • 函数 update_post($post_id, $user_id) { $result = mysql_result(mysql_query("SELECT type FROM poststuff WHERE post_id = $post_id AND user_id = $user_id") , 0) 或死(mysql_error()); if ($result !== null) { if ($result === "no" ) { $actual_count = mysql_result(mysql_query("SELECT $result FROM posts WHERE post_id = $post_id"), 0) ; mysql_query("更新posts SET $actual_count = $actual_count+1 其中post_id = $post_id AND type = '否'"); }
  • @DCV_Diego 停止并验证您之前所说的“我在 phpMyAdmin 中遇到了这个错误,只有这个查询!” 这是一个真实的陈述,其中“这个查询”是问题中的确切文本吗?
  • @DCV_Diego 好的,所以这个答案表明问题是由于呈现为空白的意外字符(或者可能根本没有显示)引起的。如果您重新输入(不复制/粘贴!)原始查询 - SELECT `type` FROM `poststuff` WHERE `post_id` = 7 AND `user_id` = 1 - 进入 phpMyAdmin 并运行它会发生什么?
  • @DCV_Diego 尝试在 SQL fiddle 中重现该问题 - 如I cannot reproduce the error
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
相关资源
最近更新 更多