且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

这个MySQL查询如何容易受到SQL注入的攻击?

更新时间:2023-02-05 21:49:18

假设已正确转义,它不会使您容易受到攻击.事实是,正确地转义比乍看起来要难,并且每次这样的查询时,您都谴责自己可以正确地转义.如果可能,请避免所有麻烦,并使用准备好的语句(或绑定的参数或参数化查询).这样做的目的是允许数据访问库正确地转义值.

Assuming it is properly escaped, it doesn't make you vulnerable. The thing is that escaping properly is harder than it looks at first sight, and you condemn yourself to escape properly every time you do a query like that. If possible, avoid all that trouble and use prepared statements (or binded parameters or parameterized queries). The idea is to allow the data access library to escape values properly.

例如,在PHP中,使用 mysqli :

For example, in PHP, using mysqli:

$db_connection = new mysqli("localhost", "user", "pass", "db");
$statement = $db_connection->prepare("SELECT thing FROM stuff WHERE id = ?");
$statement->bind_param("i", $user_id); //$user_id is an integer which goes 
                                       //in place of ?
$statement->execute();