开发者

Over Preparing a Query

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜