【问题标题】:Error: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement [duplicate]错误:mysqli_stmt::bind_result():绑定变量的数量与准备好的语句中的字段数量不匹配[重复]
【发布时间】:2016-04-08 17:18:34
【问题描述】:

运行此方法时出现此错误。谁能解释一下代码有什么问题。下面是我的功能代码

public function storeUser($name, $email, $password) {
    $uuid = uniqid('', true);
    $hash = $this->hashSSHA($password);
    $encrypted_password = $hash["encrypted"]; // encrypted password
    $salt = $hash["salt"]; // salt

    $stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
    $stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt);
    $stmt->execute();
    $result=$stmt->bind_result($uuid, $name, $email, $encrypted_password, $salt);
   // $result = $stmt->execute();
    $stmt->close();

    // check for successful store
    if ($result) {
        $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $user = $stmt->fetch();
        $stmt->close();

        return $user;
    } else {
        return false;
    }
}

【问题讨论】:

  • 你真的不应该在密码哈希上使用你自己的盐,你真的应该使用 PHP 的 built-in functions 来处理密码安全。如果您使用的 PHP 版本低于 5.5,则可以使用 password_hash() compatibility pack
  • @JayBlanchard 能给我一个简单的例子吗
  • 在 cmets 中有演示概念的链接。
  • 你为什么要在INSERT 上做->bind_result()?您的 INSERT 查询不会返回任何要绑定的结果。它应该用于SELECT
  • 这可能是,如果unique_id 列是一个AI (int),那么你不能在INSERT 上绑定它,AI 会自己做。或者,您需要使用i 而不是s 并执行VALUES('', ?, ?, ?, ?,

标签: php mysqli


【解决方案1】:

错误说你有绑定 N 个变量但没有字段,因为它是一个 insert 不返回值。 bind_result() 应该在选择语句中使用。

要修复,请删除此行:

$result=$stmt->bind_result($uuid, $name, $email, $encrypted_password, $salt);

【讨论】:

  • 我之前已经在 INSERT 上完成了结果绑定,所以这可能不是完整的修复。
  • @Fred-ii- 我会加倍,但我认为这可以解决问题,“工作”插入应该返回 6 列 LOL 我知道非常非常奇怪 xD
  • @rray 没问题。仅供参考:可以为 SELECT/INSERT 完成结果绑定,因为事实上我上周刚刚编写了一个脚本。这很棘手,但可以做到。 ;-) 干杯
  • @Fred-ii- 你能通过一些参考吗?我从来没有看到它,在 postgres 中可能插入返回“自动增量”
  • @rray 只要语法 (in MySQL)(ai'd_column, col2, col3) VALUES ('', ?, ?) 类型的东西,您就可以使用准备好的语句插入 AI 列。
猜你喜欢
  • 2014-01-06
  • 2016-12-28
  • 1970-01-01
  • 1970-01-01
  • 2016-01-23
  • 1970-01-01
  • 2018-04-07
相关资源
最近更新 更多