PDO'SQLSTATE [HY093]:参数号无效:绑定变量数与令牌数不匹配

时间:2015-02-08 03:49:35

标签: mysql pdo

我看到很多人出于同样的原因出现了同样的信息,但看不出任何似乎与我的具体情况相关的解决方案,从表面来看,这个问题非常简单。是的,我的密码等是md5,应该更改,但我不相信这是问题。

我很感激有人指出为什么第一个单行代码有效而第二个没有。在每种情况下,只有一行符合表中的标准。

<?php

// Include database class
include 'DBClass.php';

// Instantiate database.
$db = new DBClass();

//***********************
// This works
//***********************

$em = 'admin@infin8integr8.com';
$sid = 39;
$db->query("select ufname,ulname from users where uemail = :em and sub_id = :sb");
$db->bind(':em', $em);
$db->bind(':sb', $sid);
$row = $db->single();

echo $row['ufname'].'  '.$row['ulname'].'<br>';

//***************************
// This returns the error 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' in C:\wamp\www\pdo\DBClass.php on line 85
//*****************************

//Line 85 in DBClass is "return $this->stmt->execute();" in
    //public function execute(){
        //return $this->stmt->execute();
    //}

$userid = 'f534983b255eb2820bf3ba1438ddcf65';
$password = '0bfd2660362f242169d11e33f7affe0a';
$db->query = ("select ufname,ulname from users where username = :vuid and upwd = :vpwd");
$db->bind(':vuid', $userid);
$db->bind(':vpwd', $password);
$row = $db->single();

echo $row['ufname'].' * '.$row['ulname'].'<br>';

?>

The relevant class code is:-
<?php
class DBClass{

    private $host      = 'localhost';
    private $user      = '<user>';
    private $pass      = '<password>';
    private $dbname    = 'infinint_infin8';

    private $dbh;
    private $error;
    private $stmt;

    public function __construct(){
        // Set DSN
        $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
       // Set options
        $options = array(
            PDO::ATTR_PERSISTENT    => true,
            PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION,
            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");
        // Create a new PDO instanace
        try{
            $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
        }
        // Catch any errors
        catch(PDOException $e){
            $this->error = $e->getMessage();
        }
    }

    public function query($query){
        $this->stmt = $this->dbh->prepare($query);
    }   

    public function bind($param, $value, $type = null){
        if (is_null($type)) {
            switch (true) {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;
            }
        }
        $this->stmt->bindValue($param, $value, $type);
    }

    public function execute(){
        return $this->stmt->execute();
    }


    public function resultset(){
        $this->execute();
        return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function single(){
        $this->execute();
        return $this->stmt->fetch(PDO::FETCH_ASSOC);
    }   
}
?>

0 个答案:

没有答案