我正在使用php并在mysql服务器上运行sql查询。 为了防止sql注入,我正在使用mysql_real_escape_string。如何投射bigint,以防止在PHP中的SQL注入?
我也使用(INT),用于数字铸造,以下列方式:
$desired_age = 12;
$query = "select id from users where (age > ".(int)$desired_age.")";
$result = mysql_query($query);
这项工作。
但是,当变量包含较大数字时,由于它们大于int,因此它们失败。
$user_id = 5633847511239487;
$query = "select age from users where (id = ".(int)$user_id.")";
$result = mysql_query($query);
// this will not produce the desired result,
// since the user_id is actually being cast to int
有另一种方式投下大量(如BIGINT),除了使用mysql_real_escape_string的,当涉及到SQL注入预防?
这里也有一个问题$ user_id = 5633847511239487;将不会持有你的想法,并且没有办法让这个数字返回,你需要把它保存为一个字符串,而不是一个int - $ user_id ='5633847511239487'; – nathan 2010-07-08 13:58:19
而不是处理消毒输入的麻烦,使用参数化查询来避免SQL注入。 http://bobby-tables.com/php.html在PHP中向你展示了如何。 – 2012-12-12 21:56:58