dqitk20644 2014-09-22 11:28
浏览 11
已采纳

这个PDO绑定功能出了什么问题?

I am trying to simplify my database functions by creating my own class.
one of these functions is bind. it worked before but now it does some strange things
The code is:

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;
}

I am certain I formatted my data correctly.
the commented out section works, while when i try it with my custom bind function it doesnt work.
It worked before on other functions..
also it's not the sql query.. i'm certain it's in the bind function somewhere.

my final result is that EVERY column is filled with the last given parameter.
(in this context that would be :code)

e.g. this array is data

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)

The sql query generated:

INSERT INTO temp_users (salt, username, pass_hash, email, sex, birthday, code) 
VALUES(:salt, :username, :pass_hash, :email, :sex, :birthday, :code)
  • 写回答

1条回答 默认 最新

  • doubihuai8468 2014-09-22 11:36
    关注

    Replace bindParam() with bindValue(). bindParam defines a variable name to be used for the execution of the query. So, when your loop is over all variables are bound to $d, which at the point of the execution of the query has the value of the last iteration.

    By changing this to bindValue() you are setting the value $d holds at the time of the function call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch