开发者

Over Preparing a Query

开发者 https://www.devze.com 2023-03-26 14:34 出处:网络
I was wondering if one should go the extra mile in over preparing their queries. For example, a query that takes user input to make a request on the database.

I was wondering if one should go the extra mile in over preparing their queries.

For example, a query that takes user input to make a request on the database.

if(is_int($id)) {
     $mysqli->query("SELECT * FROM开发者_如何学C myTable WHERE id = '$id'");
}

vs

if(is_int($id)) {
     $mysqli->query(sprintf("SELECT * FROM myTable WHERE id = '%d'",$id));
}


There are three methods to avoid SQL injections: escaping, preparing and typing. Your example uses typing: it ensures that your variable is an integer and, thus, safe. You do not need to prepare this statement as you will never encounter an SQL injection with this particular piece of code.

0

精彩评论

暂无评论...
验证码 换一张
取 消