SQLSTATE[HY093]: Invalid parameter number: no parameters were bound, but parameters are provided

前端 未结 2 1444
春和景丽
春和景丽 2021-02-19 18:05

I\'m building a database object that joins the PDO object with the PDOStatement object in order for chaining to be available. Basically I just put the

2条回答
  •  星月不相逢
    2021-02-19 19:04

    I guess using a reference &$val instead of a value $val is what causes the issue.

    Please try this code instead:

    public function bindParam($place, $val, $dataType)
    {
        if(!$this->stmt) throw new \Exception('PDO Statement is empty');
        $this->stmt->bindParam($place, $val, $dataType);
        return $this;
    }
    

    EDIT

    My above answer is wrong.

    Try modifying the execute method:

    public function execute(array $params = array()){
        if(!$this->stmt) throw new \Exception('PDO Statement is empty');
        $this->stmt->execute();
        return $this;
    }
    

    Passing an empty array as parametre to the execute method removes all previous bindings. This is why bindParam returned true (successfully bound), yet the "no params were bound" error appeared as soon as you called execute.

提交回复
热议问题