这个PDO绑定功能有什么问题


what is goind wrong with this PDO bind function?

我正在尝试通过创建自己的类来简化我的数据库函数。
其中一个函数是绑定。它以前有效,但现在它做了一些奇怪的事情
代码为:

protected function tInsert(&$connection, $table, $data, $replaceSpecials){
    $sql = $this->createSqlQuery($table, $data);
    $stmt = $connection->prepare($sql);
    /* THIS WORKS
    $stmt->bindParam(":username", $data["username"]);
    $stmt->bindParam(":pass_hash", $data["pass_hash"]);
    $stmt->bindParam(":salt", $data["salt"]);
    $stmt->bindParam(":email", $data["email"]);
    $stmt->bindParam(":sex", $data["sex"]);
    $stmt->bindParam(":birthday", $data["birthday"]);
    $stmt->bindParam(":code", $data["code"]);
    */
    // THIS DOESNT
    $stmt = $this->bind($stmt, $data, $replaceSpecials);
    $stmt->execute();
}
private function bind($stmt, $data, $replaceSpecials){
    if ($replaceSpecials)
        foreach($data as $k => $d){
            $d = str_replace("<", "&lt;",
                str_replace(">", "&gt;", $d));
            $stmt->bindParam(":" . $k, $d);
        }
    else if (!$replaceSpecials)
        foreach($data as $k => $d) 
            $stmt->bindParam(":" . $k, $d);
    else return $this->bind($stmt, $data, false);
    return $stmt;
}

确定我正确格式化了数据。
注释掉的部分有效,而当我使用自定义绑定函数尝试它时它不起作用。
它之前在其他功能上工作过。
它也不是SQL查询。我确定它在某个地方的绑定函数中。

我的最终结果是每一列都填充了最后一个给定的参数。
(在这种情况下,这将是:代码(

例如,这个数组是数据

array (size=7)
  'salt' => string 'b3d7201e14' (length=10)
  'username' => string 'mister x' (length=8)
  'pass_hash' => string 'd930f9a672bd12c9cf94aff748ca5bd100139bd5bdc7fafbdbfc8ad4bd79ba3c' (length=64)
  'email' => string 'someone@gmail.com' (length=23)
  'sex' => string 'm' (length=1)
  'birthday' => string '25-11-1992' (length=10)
  'code' => string '1ad21a5596cb556' (length=15)

生成的 sql 查询:

INSERT INTO temp_users (salt, username, pass_hash, email, sex, birthday, code) 
VALUES(:salt, :username, :pass_hash, :email, :sex, :birthday, :code)

bindParam()替换为 bindValue() 。 bindParam 定义用于执行查询的变量名称。因此,当您的循环结束时,所有变量都绑定到 $d ,在执行查询时具有上次迭代的值。

通过将其更改为bindValue(),您将设置$d函数调用时持有的值。