gpt4 book ai didi

php - 用查询生成器/原始 MySQL 编写的查询之间有区别吗?

转载 作者:行者123 更新时间:2023-11-29 08:09:21 25 4
gpt4 key购买 nike

我被告知,我应该在 Laravel 中使用 Fluent 查询生成器,而不是原始 MySQL 查询,因为 Fluent 将防止 SQL 注入(inject)和其他类型的恶意用户输入攻击,因此这种查询方式更安全。这是真的?查询生成器不只是为了使查询代码更具可读性吗?

例如,从安全角度看,以下两种数据选择方式有区别吗?第一个仅用于使过程更漂亮吗?

DB::table('users')->where('name', Input::get('name'))->get();
DB::select(DB::raw('SELECT * FROM users WHERE name = ' . Input::get('name')));

最佳答案

Is this true? Isn't the Query Builder used only for making the query code more readable?

是的,确实如此。查询生成器通过使用 PDO 的参数化查询来帮助防止 SQL 注入(inject)攻击。

直接来自documentation for Query Builder :

Note: The Laravel query builder uses PDO parameter binding throughout to protect your application against SQL injection attacks. There is no need to clean strings being passed as bindings.

For example, is there a difference between the following two ways of data selection from a security aspect? Is the first one used only for making the process prettier?

是的,这两种方法有很大的区别。在第一个查询中,用户输入始终在插入数据库之前进行清理。在第二种方法中,您将插入原始用户输入Bobby-Tables想和你说句话!

Obligatory XKCD :

关于php - 用查询生成器/原始 MySQL 编写的查询之间有区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21955884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com