PHP - 由于双引号MYSQL查询而出错

时间:2021-01-15 22:31:28
 <?php
   $query = "SELECT * ";
   $query .= "FROM subjects ";
   $query .= "WHERE id='" . $subject_id ."' ";
   $query .= "LIMIT 1";
 ?>

The problem cause is in this line: The error : "Database query failed: 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 'LIMIT 1' at line 1"

问题原因在于这一行:错误:“数据库查询失败:您的SQL语法中有错误;请检查与您的MySQL服务器版本对应的手册,以便在第1行的'LIMIT 1'附近使用正确的语法”

So why... despite the syntax is right. Given me that error ?!

那么为什么......尽管语法是正确的。鉴于我的错误?!

3 个解决方案

#1


0  

The error message is from the execution of a line of code not shown in your question.

错误消息来自执行您问题中未显示的一行代码。

One obvious possibility is that the string value produced by this code is not being passed to MySQL. Another possibility is that $subject_id contains a string value that is being interpreted as SQL text, and the $query does not contain what you think it contains. There's a lot of other possibilities.

一个显而易见的可能性是,此代码生成的字符串值未传递给MySQL。另一种可能性是$ subject_id包含一个被解释为SQL文本的字符串值,而$ query不包含您认为它包含的内容。还有很多其他的可能性。

For debugging issues like this, we really need to identify the line of code that is actually throwing the error. (In your case, it's going to be a call to a mysql_, mysqli or PDO execute or prepare function or method.)

对于这样的调试问题,我们确实需要确定实际抛出错误的代码行。 (在你的情况下,它将调用mysql_,mysqli或PDO执行或准备函数或方法。)

What you can do is add an echo or var_dump of the ACTUAL SQL text that is being passed to MySQL, on a line immediately preceding the line that is throwing the error.

你可以做的是在抛出错误的行之前的一行上添加一个传递给MySQL的ACTUAL SQL文本的echo或var_dump。

For example, you would get this error message if a line of code after this code, and before the parse or execute call, modifies $query

例如,如果在此代码之后,在解析或执行调用之前的一行代码修改$ query,您将收到此错误消息

$query .= " LIMIT 1";

That would add a second LIMIT clause on the query, which is invalid, and would throw the same error you are getting.

这将在查询中添加第二个LIMIT子句,该子句无效,并且会抛出您获得的相同错误。

To reiterate: the lines of code posted in your question are NOT throwing an error. The lines of code you posted are simply assigning a string value to a variable. (This may be the string value that is being passed to MySQL, but we don't see that anywhere in your code.)

重申:你问题中发布的代码行不会引发错误。您发布的代码行只是将字符串值赋给变量。 (这可能是传递给MySQL的字符串值,但我们在代码中的任何位置都没有看到。)

#2


-1  

You are using 'subject_id' may be int format have some problem so use subject_id and Limit 1 alternate Limit 0,1.

您正在使用'subject_id'可能是int格式有一些问题所以使用subject_id和Limit 1 alternate Limit 0,1。

<?php
   $query = "SELECT * ";
   $query .= "FROM subjects ";
   $query .= "WHERE id=" . $subject_id;
   $query .= "LIMIT 0,1";
 ?>

#3


-4  

You don't need to concatenate the variable you can use

您不需要连接可以使用的变量

 <?php
 $query = "SELECT * ";
 $query .= "FROM subjects ";
 $query .= "WHERE id='$subject_id' ";
 $query .= "LIMIT 1";
  ?>

You have two semi-colons after your variable as well, and if your variable is a integer, it doesnt need to be in single quotes either.

你的变量后面还有两个分号,如果你的变量是一个整数,它也不需要用单引号。

#1


0  

The error message is from the execution of a line of code not shown in your question.

错误消息来自执行您问题中未显示的一行代码。

One obvious possibility is that the string value produced by this code is not being passed to MySQL. Another possibility is that $subject_id contains a string value that is being interpreted as SQL text, and the $query does not contain what you think it contains. There's a lot of other possibilities.

一个显而易见的可能性是,此代码生成的字符串值未传递给MySQL。另一种可能性是$ subject_id包含一个被解释为SQL文本的字符串值,而$ query不包含您认为它包含的内容。还有很多其他的可能性。

For debugging issues like this, we really need to identify the line of code that is actually throwing the error. (In your case, it's going to be a call to a mysql_, mysqli or PDO execute or prepare function or method.)

对于这样的调试问题,我们确实需要确定实际抛出错误的代码行。 (在你的情况下,它将调用mysql_,mysqli或PDO执行或准备函数或方法。)

What you can do is add an echo or var_dump of the ACTUAL SQL text that is being passed to MySQL, on a line immediately preceding the line that is throwing the error.

你可以做的是在抛出错误的行之前的一行上添加一个传递给MySQL的ACTUAL SQL文本的echo或var_dump。

For example, you would get this error message if a line of code after this code, and before the parse or execute call, modifies $query

例如,如果在此代码之后,在解析或执行调用之前的一行代码修改$ query,您将收到此错误消息

$query .= " LIMIT 1";

That would add a second LIMIT clause on the query, which is invalid, and would throw the same error you are getting.

这将在查询中添加第二个LIMIT子句,该子句无效,并且会抛出您获得的相同错误。

To reiterate: the lines of code posted in your question are NOT throwing an error. The lines of code you posted are simply assigning a string value to a variable. (This may be the string value that is being passed to MySQL, but we don't see that anywhere in your code.)

重申:你问题中发布的代码行不会引发错误。您发布的代码行只是将字符串值赋给变量。 (这可能是传递给MySQL的字符串值,但我们在代码中的任何位置都没有看到。)

#2


-1  

You are using 'subject_id' may be int format have some problem so use subject_id and Limit 1 alternate Limit 0,1.

您正在使用'subject_id'可能是int格式有一些问题所以使用subject_id和Limit 1 alternate Limit 0,1。

<?php
   $query = "SELECT * ";
   $query .= "FROM subjects ";
   $query .= "WHERE id=" . $subject_id;
   $query .= "LIMIT 0,1";
 ?>

#3


-4  

You don't need to concatenate the variable you can use

您不需要连接可以使用的变量

 <?php
 $query = "SELECT * ";
 $query .= "FROM subjects ";
 $query .= "WHERE id='$subject_id' ";
 $query .= "LIMIT 1";
  ?>

You have two semi-colons after your variable as well, and if your variable is a integer, it doesnt need to be in single quotes either.

你的变量后面还有两个分号,如果你的变量是一个整数,它也不需要用单引号。