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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示