php - 如何转换 bigint 以防止 php 中的 sql 注入(inject)?

标签 php mysql casting sql-injection bigint

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

相关文章:

PHP MYSQL 博客存档菜单按年和月

mysql - 在 SQL 内连接中使用别名

mysql - 如何优化该查询?

php - jquery ajax文件上传

php - 从评论框发送电子邮件

php - MySQL .. 使用全文搜索还是使用 Like?什么是更好的?

swift - 需要时如何将 Float 转换为 Int?

swift - 如果这个类是通用的,如何检查一个对象是否是给定类的实例?

c# 如何转换泛型类型

php - 无法在 PHP 5.3.5 中加载自定义 PHP 扩展