我的MySql Select查询有什么问题吗


Is there anything wrong with my MySql Select query?

我正试图使用Ajax请求从数据库中获取一些结果:

SELECT * 
FROM manifest_child 
WHERE manifest_id = $manifest_id
AND s_no = $sub_id
AND branch_code = '$branch_code'
        ";

一切看起来都很好,但当我在其中传递一些值时,它给出了一个错误:

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 'AND s_no = 3 AND branch_code = 'KHI'' at line 3

我陷入了困境,任何人都请帮帮我,提前谢谢。

可能是换行符,请尝试此

$que = "SELECT * FROM manifest_child".
        "WHERE   manifest_id     = $manifest_id".
        "AND     s_no            = $sub_id".
        "AND     branch_code     = '$branch_code'";

我认为manifest_ids_no不是整数,所以用引号将它们括起来。试试这个

$que = "SELECT * FROM manifest_child 
        WHERE manifest_id = '$manifest_id' 
        AND s_no = '$sub_id' 
        AND branch_code = '$branch_code'";

您的查询对于SQL注入是不安全的你必须这样做:

$que = mysql_escape_string($que);
$que = "SELECT * FROM manifest_child 
    WHERE   manifest_id     = $manifest_id 
    AND     s_no            = $sub_id 
    AND     branch_code     = '$branch_code'
    ";

这样,您的代码将更加安全,并且您将摆脱此错误。我认为这个错误是因为你在传递参数时放了另一个'(qute)。

我得到了我的答案,它给出了一个mysql错误,因为$manifest_id得到了一个null值,因此WHERE子句没有得到满足,我没有注意到,因为mysql错误没有在错误中显示null部分。我试着用不同的方式查询,错误仍然存在,但用了不同的方式:

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 '=AND s_no = 3 AND branch_code = 'KHI'' at line 3

注意AND关键字前面的等号。谢谢大家。:)