When running my PHP script It keeps giving me the error
在运行我的PHP脚本时,它总是给我错误。
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 '1' at line 1
在SQL语法中有一个错误;请检查与您的MySQL服务器版本对应的手册,以便在第1行中使用接近“1”的语法。
This is my sql code I have other than selecting from the table. I have commented out all of this and not gotten an error, so I'm assuming its occuring in this block of code.
这是我从表中选择的sql代码。我已经注释掉了所有这些并没有出错,所以我假设它发生在这段代码中。
if($status === 1){
$sqlQ = mysql_query("UPDATE tablename SET status=1 WHERE steam64='$id'");
if(!mysql_query($sqlQ, $con)){
die('Error: ' . mysql_error());
}
}else if($status !== 1){
$sqlQ = mysql_query("UPDATE tablename SET status=2 WHERE steam64='$id'");
if(!mysql_query($sqlQ, $con)){
die('Error: ' . mysql_error());
}
}
What is really confusing me is the line 1 part.
真正让我困惑的是第一部分。
5 个解决方案
#1
3
You're violating the DRY principle big time. Why not something like...
你违反了干原则。为什么不像……
$statusValue = ($status === 1) ? 1 : 2;
$sqlQuery = mysql_query("UPDATE `14d2_group` SET `status` = $statusValue WHERE `steam64` = '$id'"):
UPDATE 2: It looks like there's a need for additional clarification.
更新2:看起来需要进一步的澄清。
mysql_query function doesn't only create a query: it actually sends in to MySQL - and returns the result. In case of UPDATE it will return FALSE if query has failed. That's why you shouldn't call mysql_query twice, as you did in the original example.
mysql_query函数不仅创建一个查询:它实际上发送到MySQL——并返回结果。如果发生更新,如果查询失败,它将返回FALSE。这就是为什么不应该像在原示例中那样调用mysql_query两次。
You can check how many lines were actually updated with mysql_affected_rows function.
您可以通过mysql_affected_rows函数检查实际更新了多少行。
UPDATE 3: Finally get it. ) That was the reason error appeared: you tried to call mysql_query with result of the last update query. Which was, as TRUE converted to String, just '1'. )
更新3:最终得到它。这就是出现错误的原因:您试图用上次更新查询的结果调用mysql_query。也就是说,它被转换为字符串,只有'1'。
#2
2
You're using the result from one query as a query itself.
您将一个查询的结果作为查询本身使用。
What you probably wanted to do is:
你可能想做的是:
if($status === 1){
$sqlQ = mysql_query("UPDATE tablename SET status=1 WHERE steam64='$id'");
if (!$sqlQ) {
die('Error: ' . mysql_error());
}
}
else {// no need for your if-statement here because it would always be true
$sqlQ = mysql_query("UPDATE tablename SET status=2 WHERE steam64='$id'");
if(!$sqlQ){
die('Error: ' . mysql_error());
}
}
#3
1
"Line 1" corresponds to line 1 of the query, not the script invoking it. To add the line of the script invoking it, use:
“第1行”对应的是查询的第1行,而不是调用它的脚本。要添加调用它的脚本的行,请使用:
die('Error: ' . mysql_error() . ' in ' . $_SERVER['PHP_SELF'] . ' on line ' . __LINE__ );
As for the query, I don't really see anything jumping out at me. The only suggestion I have right now is to always enclose field names in backticks, just in case they're keywords (it also makes them clearer to read)
至于查询,我真的看不到有什么东西跳出来。我现在唯一的建议是,总是在backticks中包含字段名,以防它们是关键字(它也使阅读变得更清晰)
Also, your else if
is redundant. If $status === 1
doesn't run, then clearly $status !== 1
must be true.
另外,如果是多余的。如果$status == 1不运行,那么显然$status !== 1必须为真。
#4
0
Because of type casting, status=1
is not a problem. I'm assuming $id
has some probrem. Once change $id
to other safe value (1, 'foo'...) then check it works or not.
由于类型转换,状态=1不是问题。假设$id有一些probrem。一旦将$id更改为其他安全值(1,'foo'…),检查它是否有效。
#5
-1
The 'line 1' part is SQL saying that hte message it recieved had an error on line 1 -- the first line of the command that SQL tried to process.
“第1行”部分是SQL,说它收到的hte消息在第1行有一个错误——SQL试图处理的命令的第一行。
If I had to make a guess, status isn't set to a number type, so you need to put quotes around it so that SQL knows it's being passed a variable.
如果我必须猜测,状态不是设置为数字类型,所以你需要在它周围加上引号,这样SQL就知道它被传递了一个变量。
Edit: OK, the other solution might be right too. We both made different assumptions about your data structure, and I think his is better. Try it first.
编辑:好的,另一个解决方案也可能是正确的。我们对你的数据结构做了不同的假设,我认为他的更好。先试一试。
#1
3
You're violating the DRY principle big time. Why not something like...
你违反了干原则。为什么不像……
$statusValue = ($status === 1) ? 1 : 2;
$sqlQuery = mysql_query("UPDATE `14d2_group` SET `status` = $statusValue WHERE `steam64` = '$id'"):
UPDATE 2: It looks like there's a need for additional clarification.
更新2:看起来需要进一步的澄清。
mysql_query function doesn't only create a query: it actually sends in to MySQL - and returns the result. In case of UPDATE it will return FALSE if query has failed. That's why you shouldn't call mysql_query twice, as you did in the original example.
mysql_query函数不仅创建一个查询:它实际上发送到MySQL——并返回结果。如果发生更新,如果查询失败,它将返回FALSE。这就是为什么不应该像在原示例中那样调用mysql_query两次。
You can check how many lines were actually updated with mysql_affected_rows function.
您可以通过mysql_affected_rows函数检查实际更新了多少行。
UPDATE 3: Finally get it. ) That was the reason error appeared: you tried to call mysql_query with result of the last update query. Which was, as TRUE converted to String, just '1'. )
更新3:最终得到它。这就是出现错误的原因:您试图用上次更新查询的结果调用mysql_query。也就是说,它被转换为字符串,只有'1'。
#2
2
You're using the result from one query as a query itself.
您将一个查询的结果作为查询本身使用。
What you probably wanted to do is:
你可能想做的是:
if($status === 1){
$sqlQ = mysql_query("UPDATE tablename SET status=1 WHERE steam64='$id'");
if (!$sqlQ) {
die('Error: ' . mysql_error());
}
}
else {// no need for your if-statement here because it would always be true
$sqlQ = mysql_query("UPDATE tablename SET status=2 WHERE steam64='$id'");
if(!$sqlQ){
die('Error: ' . mysql_error());
}
}
#3
1
"Line 1" corresponds to line 1 of the query, not the script invoking it. To add the line of the script invoking it, use:
“第1行”对应的是查询的第1行,而不是调用它的脚本。要添加调用它的脚本的行,请使用:
die('Error: ' . mysql_error() . ' in ' . $_SERVER['PHP_SELF'] . ' on line ' . __LINE__ );
As for the query, I don't really see anything jumping out at me. The only suggestion I have right now is to always enclose field names in backticks, just in case they're keywords (it also makes them clearer to read)
至于查询,我真的看不到有什么东西跳出来。我现在唯一的建议是,总是在backticks中包含字段名,以防它们是关键字(它也使阅读变得更清晰)
Also, your else if
is redundant. If $status === 1
doesn't run, then clearly $status !== 1
must be true.
另外,如果是多余的。如果$status == 1不运行,那么显然$status !== 1必须为真。
#4
0
Because of type casting, status=1
is not a problem. I'm assuming $id
has some probrem. Once change $id
to other safe value (1, 'foo'...) then check it works or not.
由于类型转换,状态=1不是问题。假设$id有一些probrem。一旦将$id更改为其他安全值(1,'foo'…),检查它是否有效。
#5
-1
The 'line 1' part is SQL saying that hte message it recieved had an error on line 1 -- the first line of the command that SQL tried to process.
“第1行”部分是SQL,说它收到的hte消息在第1行有一个错误——SQL试图处理的命令的第一行。
If I had to make a guess, status isn't set to a number type, so you need to put quotes around it so that SQL knows it's being passed a variable.
如果我必须猜测,状态不是设置为数字类型,所以你需要在它周围加上引号,这样SQL就知道它被传递了一个变量。
Edit: OK, the other solution might be right too. We both made different assumptions about your data structure, and I think his is better. Try it first.
编辑:好的,另一个解决方案也可能是正确的。我们对你的数据结构做了不同的假设,我认为他的更好。先试一试。