我正在使用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