php - 下面的代码可以有效防止 sql 注入吗 ?

 不是一点都不很帅_973 发布于 2022-11-21 23:35

下面的代码可以有效防止 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;

?>
2 个回答
  • 你的代码完全可以防止SQL注入,因为PDO就是SQL预处理的。

    2022-11-22 01:12 回答
  • 建议这样写, 能更有效的防注入

    ......
    
    $sql="select * from table where username = ?";
    
    ......
    
    
        while ($row = $query->fetch(PDO::FETCH_ASSOC) && $row['pass'] == $pass) {
            print_r($row);
        }
    
    2022-11-22 01:12 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有