我正在使用 php 并在 mysql 服务器上运行 sql 查询。 为了防止sql注入(inject),我使用了mysql_real_escape_string。
我还通过以下方式使用 (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
除了使用mysql_real_escape_string,还有什么方法可以转大数(比如BIGINT),什么时候防止sql注入(inject)?
最佳答案
如果您自己生成用户 ID,则无需为 MySQL 转换它,因为不会出现 SQL 注入(inject)或其他字符串问题。
如果是用户提交的值则使用filter_var()
(或 is_numeric()
)来验证它是一个数字而不是一个字符串。
关于php - 如何转换 bigint 以防止 php 中的 sql 注入(inject)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3204186/