简体   繁体   English

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

[英]What is wrong with this PHP MySQL Query?

I keep getting the following error: 我不断收到以下错误:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 检查与您的MySQL服务器版本相对应的手册,以在第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和$ end变量的代码:

$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? 问题出在Internet Explorer 9上。有人知道为什么吗?

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. OP在此答案的注释中指出,开始和结束值未出现在输出中,并且这些值由GET请求提供。

The output would give us a better idea what the issue is, but I recommend using sprintf to parameterize the query: 输出将使我们更好地了解问题所在,但我建议使用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"]);

Check the return value of mysql_real_escape_string($myuid) . 检查mysql_real_escape_string($myuid)的返回值。 Maybe it is not returning anything. 也许它没有返回任何东西。

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). 然后,我将检查mysql_real_escape_string($ myuid)的值。 ensure you have escaped any quotes "'" and that the value is already not quoted. 确保您已转义了所有引号“'”,并且该值尚未被引用。

I had the exact same trouble few minutes ago and that trouble was only on Internet explorer too. 几分钟前我也遇到了同样的麻烦,那麻烦也只在Internet Explorer上。 On Chrome, Firefox work just fine. 在Chrome上,Firefox可以正常工作。

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. <meta http-equiv="refresh" content="0;url=fileX.php" />正如我所看到的,IE不喜欢使用小写形式的url。 By setting it this way <meta http-equiv="refresh" content="0;**URL**=fileX.php" /> it works just fine. 通过这样设置<meta http-equiv="refresh" content="0;**URL**=fileX.php" />它可以正常工作。

First time it runs ok. 第一次运行正常。 But then it refresh and has 0 valu so that why we are getting that error from SQL. 但是,它刷新后的值为0,因此我们为什么要从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. 在我看来,这不是服务器端错误,而是IE,它不希望像其他所有浏览器一样读取代码。

Some of your quotes are all at an angle; 您的某些报价全都是成角度的。 they need to be like this: ' not: ` 他们需要这样:'不是:`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM