参数号无效:绑定变量数与令牌数不匹配

时间:2016-01-05 15:38:11

标签: php database data-binding pdo code-snippets

我刚学会使用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;
}

1 个答案:

答案 0 :(得分:1)

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

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

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);
}