这个 PHP MySQL 查询有什么问题?

发布于 2024-10-15 20:07:16 字数 589 浏览 6 评论 0原文

我不断收到以下错误:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行 '' 附近使用的正确语法

对于以下查询:

$query = "SELECT `Gift`, `Type` 
            FROM `gifts` 
           WHERE `User`= '".mysql_real_escape_string($myuid)."' 
           LIMIT ".$start.", ".$end;

这是我用来获取 $start 和 $end 变量的代码:

$start = $_GET['start'];
if($start = "") {
  $start = 0;
}
$end = $_GET['end'];
if($end = "") {
  $end = 7;
}

我发现问题:

我在另一个浏览器中测试了这个脚本,它工作得很好。问题出在 Internet Explorer 9 上。有人知道为什么吗?

I keep getting 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 '' at line 1

For the following query:

$query = "SELECT `Gift`, `Type` 
            FROM `gifts` 
           WHERE `User`= '".mysql_real_escape_string($myuid)."' 
           LIMIT ".$start.", ".$end;

Here is the code I use to GET the $start and $end Variables:

$start = $_GET['start'];
if($start = "") {
  $start = 0;
}
$end = $_GET['end'];
if($end = "") {
  $end = 7;
}

I Found The Problem:

I tested this script in another browser, and it worked just fine. The problem is something with Internet Explorer 9. Anyone know why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

生生漫 2024-10-22 20:07:17

检查mysql_real_escape_string($myuid)的返回值。也许它没有返回任何东西。

Check the return value of mysql_real_escape_string($myuid). Maybe it is not returning anything.

墨小沫ゞ 2024-10-22 20:07:17

提供的错误应该给你一个线索 -

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 '' at line 1

它说“”附近有一个错误。我将检查 mysql_real_escape_string($myuid) 的值。确保您已转义任何引号“'”并且该值尚未被引用。

the error supplied should give you a clue -

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 '' at line 1

It is saying that there is an error near ''. I would be checking then value of mysql_real_escape_string($myuid). ensure you have escaped any quotes "'" and that the value is already not quoted.

明明#如月 2024-10-22 20:07:17

几分钟前我也遇到了同样的问题,而且这个问题也只出现在 Internet Explorer 上。在 Chrome 上,Firefox 工作得很好。

我知道随着数据库中的信息更新,脚本工作正常。

我这边的麻烦是处理后的重定向

正如我所看到的,IE 不喜欢小写的 url。
通过这样设置它就可以正常工作。

第一次运行没问题。但随后它刷新并具有 0 值,这就是我们从 SQL 中收到该错误的原因。在我看来,这不是服务器端错误,而是 IE 不想像所有其他浏览器一样读取代码。

I had the exact same trouble few minutes ago and that trouble was only on Internet explorer too. On Chrome, Firefox work just fine.

I know the script works fine as the Info is updated in the Database.

The trouble in my side was the redirection after the process.
<meta http-equiv="refresh" content="0;url=fileX.php" />
And as I can see IE does not like to have url in lower case.
By setting it this way<meta http-equiv="refresh" content="0;**URL**=fileX.php" />it works just fine.

First time it runs ok. But then it refresh and has 0 valu so that why we are getting that error from SQL. At my eye this is not a server side error but IE that does not want to read the code as all other browser.

酷到爆炸 2024-10-22 20:07:17

你的一些引言都是有角度的;他们需要像这样:'而不是:`

Some of your quotes are all at an angle; they need to be like this: ' not: `

冰之心 2024-10-22 20:07:16

您应该在将查询发送到数据库之前将其打印出来:

$start = $_GET["start"];
$end = $_GET["end"];

$query = "SELECT `Gift`, `Type` 
            FROM `gifts` 
           WHERE `User`= '".mysql_real_escape_string($myuid)."' 
           LIMIT ".$start.", ".$end;

echo $query;

更新

OP 在对此答案的注释中提供了 start & 。最终值不会出现在输出中,并且这些值由 GET 请求提供。

输出将使我们更好地了解问题是什么,但我建议使用 sprintf 参数化查询:

$query = sprintf("SELECT g.gift,
                         g.type
                    FROM GIFTS g
                   WHERE g.user = '%s'
                   LIMIT %u, %u",
                  mysql_real_escape_string($myuid),
                  $_GET["start"], 
                  $_GET["end"]);

You should print the query out before it's sent to the database:

$start = $_GET["start"];
$end = $_GET["end"];

$query = "SELECT `Gift`, `Type` 
            FROM `gifts` 
           WHERE `User`= '".mysql_real_escape_string($myuid)."' 
           LIMIT ".$start.", ".$end;

echo $query;

Update

The OP provides in the comments to this answer that the start & end values aren't appearing in the output, and the values are supplied by a GET request.

The output would give us a better idea what the issue is, but I recommend using sprintf to parameterize the query:

$query = sprintf("SELECT g.gift,
                         g.type
                    FROM GIFTS g
                   WHERE g.user = '%s'
                   LIMIT %u, %u",
                  mysql_real_escape_string($myuid),
                  $_GET["start"], 
                  $_GET["end"]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文