0

我已经存储了用户名、密码和其他变量,但在这一行中我不断收到错误“无效的参数编号:绑定变量的数量与令牌的数量不匹配”:

$database->query(
    'INSERT INTO users_inactive(verCode, username, password, email, date, type) 
    VALUES (:vercode, :username, :password, :email, :date, :type)', 
    array(
        ':vercode' => $verCode, 
        ':username' => $username, 
        ':password' => $password, 
        ':email' => $email, 
        ':date' => $date, 
        ':type'=>'customer')
);

有什么问题吗?我已确保这些列中的每一列都在我的user_inactive表中可用。

这是 $database 包装函数:

    public function query($query, $bind = null, $fetch = 'FETCH_ASSOC') {
    /* Prepare the query statement */
    $this->statement = $this->pdo->prepare($query);
    /* Bind each value supplied from $bind */
    if($bind != null) {
        foreach($bind as $select => $value) {
            /* For each type of value give the appropriate param */
            if(is_int($value)) {
                $param = PDO::PARAM_INT; 
            } elseif(is_bool($value)) {
                $param = PDO::PARAM_BOOL;
            } elseif(is_null($value)) {
                $param = PDO::PARAM_NULL;
            } elseif(is_string($value)) {
                $param = PDO::PARAM_STR;
            } else {
                $param = FALSE;
            }
            /* Bid value */
            if($param) {
                $this->statement->bindValue($select, $value, $param);
            }
        }
    }
    /* Execute Query & check for any errors */
    if(!$this->statement->execute()){
        $result = array(
            1 => 'false',
            2 => '<b>[DATABASE] Error - Query:</b> There was an error in sql syntax',
        );
        return $result;
    }
    /* Return all content */
    if($fetch == 'FETCH_ASSOC') {
        $result = $this->statement->fetch(PDO::FETCH_ASSOC);
    } elseif($fetch == 'FETCH_BOTH') {
        $result = $this->statement->fetch(PDO::FETCH_BOTH);
    } elseif($fetch == 'FETCH_LAZY') {
        $result = $this->statement->fetch(PDO::FETCH_LAZY);
    } elseif($fetch == 'FETCH_OBJ') {
        $result = $this->statement->fetch(PDO::FETCH_OBJ);
    } elseif($fetch == 'fetchAll') {
        $result = $this->statement->fetchAll();
    }
    return $result;
}
  }

取自 Tutis 登录。

4

3 回答 3

0

只是一些线索,您可能缺少一些冒号。在这里查看

PHP Mysql PDO 绑定变量数与令牌数不匹配

或者您的冒号上可能有一些区分大小写的内容

于 2013-04-28T15:46:14.847 回答
0

错误似乎在您的包装函数中。PDO::PARAM_NULL == 0,所以这个值不会通过

if($param) {
    ...
}

因此,您不要为 NULL 值调用 bindValue()。相反,使用:

if ($param !== FALSE) {
    ...
}

编辑-再次阅读您的代码,这将是 foreach() 块的更好代码,将来会产生更少的神秘错误消息:)

    foreach($bind as $select => $value) {
        /* For each type of value give the appropriate param */
        if(is_int($value)) {
            $param = PDO::PARAM_INT; 
        } elseif(is_bool($value)) {
            $param = PDO::PARAM_BOOL;
        } elseif(is_null($value)) {
            $param = PDO::PARAM_NULL;
        } elseif(is_string($value)) {
            $param = PDO::PARAM_STR;
        } else {
            // Report error about invalid type and return from the function
            ...
        }
        // we should bind every value from $bind array, unconditionally!
        $this->statement->bindValue($select, $value, $param);
    }
于 2013-04-28T15:55:29.820 回答
-1

试一试:

<?php
$sql = $database->prepare(
'INSERT INTO users_inactive(verCode, username, password, email, date, type) 
VALUES (:vercode, :username, :password, :email, :date, :type)');
$sql->execute(array(
    'vercode' => $verCode, 
    'username' => $username, 
    'password' => $password, 
    'email' => $email, 
    'date' => $date, 
    'type'=>'customer')
);
于 2013-04-28T15:48:32.733 回答