【问题标题】:exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number带有消息“SQLSTATE [HY093]”的异常“PDOException”:参数号无效
【发布时间】:2016-09-18 10:29:58
【问题描述】:

这看起来很简单,但由于某些原因我无法让它工作......我需要我能得到的所有帮助

 function register_user($email, $password,  $gender, $phone_number){
    $this->sql = "insert into user
                  (
                  email_addr, phone_number, password,
                  gender, activation_code, isMobileVerified,
                  last_login_date, unix_sign_up_time, sign_up_date
                  ) VALUES
                  (
                  :email, :phone_number, :password,
                  :gender, :activation_code, :isMoobileVerified,
                  :last_login, :time_unix, NOW()
                  )";
    $this->prepare($this->sql);
    $this->bind(':email', $email);
    $this->bind(':phone_number', $phone_number);
    $this->bind(':password', $password);
    $this->bind(':gender', $gender);
    $this->bind(':activation_code', sha1($password));
    $this->bind(':isMobileVerified', 0);
    $this->bind(':last_login', time());
    $this->bind(':time_unix', time());

    $this->execute();

    return $this->lastInsertedId();
}

当我像这样运行这个函数时

try{
    echo "<br>" . $i->register_user('myMail@register.com', 'password', 'male', '2348020000007');
}catch (PDOException $e) {
    //todo: logging function or mail to dev goes here
    echo $e ."<br>". $e->getMessage();
}

我收到这个错误

异常 'PDOException' 与消息 'SQLSTATE[HY093]: 无效参数号:参数未定义'在 C:\wamp\www\ecommerce\system\model\class.ecommerce.php:215 堆栈跟踪:#0 C:\wamp\www\ecommerce\system\model\class.ecommerce.php(215): PDOStatement->bindValue(':isMobileVerifi...', true, 5) #1 C:\wamp\www\ecommerce\ system\model\class.ecommerce.php(281): ukorJidechi\db_handler->bind(':isMobileVerifi...', true) #2 C:\wamp\www\ecommerce\namespace_test.php(51): ukorJidechi\ ecommerce_user->register_user('myMail@register...', 'password', 'male', '2348020000007') #3 {main} SQLSTATE[HY093]:参数号无效:参数未定义

这是我的自定义绑定方法

function bind($placeholder, $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($placeholder, $value, $type);
}

谁能告诉我我做错了什么?请。

【问题讨论】:

    标签: php mysql sql pdo


    【解决方案1】:

    在此处查看此声明,

    $this->bind(':unix_sign_up_time', time());
    

    并将上述语句与$this-&gt;sql进行比较

    $this->sql = "insert into user
                  (
                  ...
                  :last_login, :time_unix, NOW()
                                ^^^^^^^^^
                  )";
    

    所以你的bind() 声明应该是,

    $this->bind(':time_unix', time());
    

    已编辑:

    $this-&gt;sql 中查看此行,

    $this->sql = "insert into user
                  (
                  ...
                  :gender, :activation_code, :isMoobileVerified,
                                              ^^^^^^^^^^^^^^^^^^
                  ...
                  )";
    

    应该是:isMobileVerified

    【讨论】:

    • 感谢您发现这一点,我已经更正了我的代码并编辑了我的问题,但我仍然收到相同的错误消息。
    • @Ukor bind() 是您的自定义类方法吗?因为如果不是,那么应该是bindParam(),而不是bind()
    • 这是我的自定义类方法,...我将更新我的问题以显示我的自定义绑定方法。我更新了我的问题以显示我的自定义绑定方法。感谢您的宝贵时间。
    • @Ukor 通过查看您的错误消息,似乎错误在于此语句$this-&gt;bind(':isMobileVerified', 0);。将与0 不同的值传递给此方法,看看它是否有效。它将进一步缩小您的问题范围。
    • @Ukor 我已经更新了我的答案。请参阅我回答的已编辑部分。
    猜你喜欢
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    相关资源
    最近更新 更多