参数编号无效:绑定变量的数量与标记的数量不匹配

Invalid parameter number: number of bound variables does not match number of tokens

我刚学会使用 PHP PDO,遇到以下问题:

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens 

错误参考这段代码:

if( $this->_query->execute() ){

这是我的代码:

public function query($dbUse='', $sql, $params = array(), $datatypes = array(), $orderby='', $limit=''){
    $this->_error = false; //always first initialize to false

    /* Check which DB will be used */
    $this->_pdo = $this->_pdoPostgres;

    $FlagSelectWithCount = ( substr($sql, 0, 6) == 'SELECT' ? true : false );

    if( $FlagSelectWithCount ){ // received SELECT statement
        if( $this->_query = $this->_pdo->prepare( "SELECT COUNT(*) as computedrow FROM ( {$sql} ) AS X", array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL) ) ){

            $x = 1;
            if( count($params) ){
                foreach($params as $param){
                    $this->_query->bindValue($x, $param);
                    $x++;
                }
            }

            if( $this->_query->execute() ){
                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);

                foreach ($this->_results as $obj){
                    $this->_count = $obj->computedrow;
                }

                if($this->_count){
                    if( $this->_query = $this->_pdo->prepare( $sql . $orderby , array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL) ) ){

                        $x = 1;
                        if( count($params) ){
                            foreach($params as $param){
                                $this->_query->bindValue($x, $param);
                                $x++;
                            }
                        }
                        if( $this->_query->execute() ){
                            $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                        }else {
                            $this->_error = true;
                        }
                    }
                }
                else{
                    $this->_count = 0;
                }

                //$rows = $this->_query->fetchColumn();
                //$this->_count = count($rows); //for select

            } else {
                $this->_error = true;
            }
        }
    }
    else{

        if( $this->_query = $this->_pdo->prepare( $sql ) ){

            $x = 1;
            if( count($params) ){

                foreach($params as $param){
                    $this->_query->bindValue($x, $param, $datatypes[$x-1]);

                    $x++;
                }
            }
            //$this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            //$this->_pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

            if( $this->_query->execute() ){
                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();
            } else {
                $this->_error = true;
            }
        }
    }
    /*
    if( substr($sql, 0, 6) == 'SELECT' ){
        $sql = "SELECT COUNT(*) FROM ( {$sql} ) AS X";
    }

    //$sql = "SELECT * FROM user_profile WHERE user_name='husni'";
    if( $this->_query = $this->_pdo->prepare( "SELECT COUNT(*) FROM ( {$sql} ) AS X", array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL) ) ){
        //echo "SELECT COUNT(*) FROM ( {$sql} ) AS X";
        $x = 1;
        if( count($params) ){
            foreach($params as $param){
                $this->_query->bindValue($x, $param);
                $x++;
            }
        }

        if( $this->_query->execute() ){

            $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
            echo $rows = $this->_query->fetchColumn();
            echo ' tryCount'.count($rows);
            $this->_count = count($rows); //for select
            //echo ' countReturned--'.$this->_count = $this->_query->rowCount(); //for other than select
            //echo '--';
        } else {
            $this->_error = true;
        }
    }
    */

    return $this;
}

这意味着 $sql 字符串中的问号(未命名标记)少于 $param[=27 中的元素=] 传递给函数的数组。

您可以在您的代码中添加此测试,当它们不相等时将输出一条消息:

if (substr_count($sql, "?") != count($param)) {
    printf ("Error: SQL has %d tokens, while %d parameters were provided.",
            substr_count($sql , "?"), count($param));
}

该测试不是防弹的,因为您可能在字符串文字中有问号:这些会被错误地计算。

但这可以用于调试您的代码。

注意:您可以使用 $x =>

编写循环

像这样:

foreach($params as $x => $param){
    $this->_query->bindValue($x+1, $param);
}