下面的代码可以有效防止 sql 注入吗 ?
大家一般是怎么做的 .
setattribute(pdo::attr_emulate_prepares, false); //禁用prepared statements的仿真效果$dbh->exec(set names 'utf8'); $sql=select * from table where username = ? and password = ?;$query = $dbh->prepare($sql); $exeres = $query->execute(array($username, $pass)); if ($exeres) { while ($row = $query->fetch(pdo::fetch_assoc)) { print_r($row); } }$dbh = null;?>
回复内容: 下面的代码可以有效防止 sql 注入吗 ?
大家一般是怎么做的 .
setattribute(pdo::attr_emulate_prepares, false); //禁用prepared statements的仿真效果$dbh->exec(set names 'utf8'); $sql=select * from table where username = ? and password = ?;$query = $dbh->prepare($sql); $exeres = $query->execute(array($username, $pass)); if ($exeres) { while ($row = $query->fetch(pdo::fetch_assoc)) { print_r($row); } }$dbh = null;?>
建议这样写, 能更有效的防注入
......$sql=select * from table where username = ?;...... while ($row = $query->fetch(pdo::fetch_assoc) && $row['pass'] == $pass) { print_r($row); }
你的代码完全可以防止sql注入,因为pdo就是sql预处理的。