2013-07-31 25 views
0

是否有任何mysql语法来阻止查询被用户输入追加?如果查询是否有这样的mysql语法来结束/限制mysql查询?

SELECT * 
    FROM `username` 
    WHERE `type` = 'client' 
    END/LIMIT/whatever syntax here; 

查询应在'client'停止,所以如果用户试图通过添加OR像这样添加自己的查询:

SELECT * 
    FROM `username` 
    WHERE `type` = 'client' 
    OR 1 = 1; 

将无法​​正常工作。提前致谢。

回答

0

我会假设你正在使用PHP,并且'client'是通过用户的$ _POST作为参数接收的。在这种情况下,我认为你想要做的就是防止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和适应它为您的需求。

+0

感谢您的回复。是的,这是PHP。 – sg552