PDO prepared statements [closed]

南笙酒味 提交于 2019-12-13 23:52:17

问题


I know that PDO prepared statements should be used to avoid SQL injection. Must it always have this format:

$stmt = $db->prepare('SELECT * FROM table where id = :id');
$stmt->execute( array(':id' => $_GET['id']) );

or will any of the following formats negate SQL injection too?

VERSION 1

$queryString = "SELECT * FROM table WHERE id = ".$_GET['id'];
$stmt= $db->prepare($queryString);  
$stmt->execute();
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);

VERSION 2

$stmt = $db->query("SELECT * FROM table WHERE id = ".$_GET['id']);
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);

回答1:


You have to bind your variables like you do in your first code. The Version 1 and Version 2 codes are both INSECURE.



来源:https://stackoverflow.com/questions/17076202/pdo-prepared-statements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!