dpdrtj1075 2011-12-05 18:18
浏览 71
已采纳

我可以将参数绑定到PDO语句作为比较运算符吗?

Is this code

class opinion
{
   private $dbh;
   var $opinionid,$opinion,$note,$actorid,$dateposted;
   var $isnew=FALSE;
   function loadby($column,$value,$operator="="){
       $dbh = new PDO(I deleted parameters here);
       $statement=$dbh->prepare("select * from fe_opinion where :column :operator :value");
       $statement->bindParam(":column", $column);
       $statement->bindParam(":value", $value);
       $statement->bindParam(":operator", $operator); //UNSURE, DOUBTFUL
       $statement->bindColumn("opinionid", $this->opinionid);
       $statement->bindColumn("opinion", $this->opinion);
       $statement->bindColumn("note", $this->note);
       $statement->bindColumn("actorid", $this->actorid);
       $statement->bindColumn("dateposted", $this->dateposted);
       $statement->fetch();
       return $statement->rowCount(); //please be 1
   }
}

injection safe?

       $statement->bindParam(":operator", $operator); //UNSURE, DOUBTFUL

Can I bind a parameter to a PDO statement as a comparison operator?

  • 写回答

4条回答 默认 最新

  • duanna2026 2011-12-05 18:25
    关注

    No, you cannot bind operators like that. As a workaround, you can dynamically create the "base" SQL query and use an operator whitelist (which is quite appropriate) to remain safe from injection:

    function loadby($column,$value,$operator="="){ 
       $dbh = new PDO(...); 
       $operator = getOperator($operator);
       if(!$operator) {
           // error handling
       }
       $statement=$dbh->prepare("select * from fe_opinion where :column $operator :value");
       // the rest like you already do it
    } 
    
    function getOperator($operator) {
       $allowed_ops = array('=', '<', '>'); // etc
       return in_array($operator, $allowed_ops) ? $operator : false;
    }
    

    Apart from this, the rest is fine and injection-proof "by definition".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作