是否有任何mysql语法可以阻止查询被用户输入追加?如果查询是
SELECT *
FROM `username`
WHERE `type` = 'client'
END / LIMIT / whatever syntax here;
查询应该停在'client'
,因此如果用户尝试添加OR
这样的代码来添加自己的查询:
SELECT *
FROM `username`
WHERE `type` = 'client'
OR 1 = 1;
它不起作用。提前谢谢。
答案 0 :(得分:0)
我将假设您使用的是PHP,并且通过$ _POST从用户那里收到'client'作为参数。在这种情况下,我认为你想要做的是阻止SQL注入。如果是这样,您应该使用mysql_real_escape_string()来清理输入参数来解决它:
// This could be supplied by a user, for example
$type = $POST['type'];
// Formulate Query
// This is the best way to perform an SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT * FROM username
WHERE type='%s',
mysql_real_escape_string($type));
// Perform Query
$result = mysql_query($query);
...
我从mysql-query reference中取样并根据您的需要进行调整。