dongzhi6927 2016-02-10 15:20
浏览 173
已采纳

PDO多参数绑定用于准备语句

I have a prepared statement in PDO with multiple parameters, is there a way to bind the parameters in group, or chain the calls so as to avoid tediously calling bindParam for every item ?

What it looks like right now (I have even more parametrized queries elsewhere):

$stmt = $pdo->prepare("INSERT INTO users (name, pass, mail, created, timezone_name, hash_pass, salt) VALUES (:name, :pass, :mail, :created, :timezone, :hashed, :salt") ;

$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->bindParam(':pass', $pass, PDO::PARAM_STR);
$stmt->bindParam(':mail', $mail, PDO::PARAM_STR);
$stmt->bindParam(':created', $date, PDO::PARAM_INT);
$stmt->bindParam(':timezone', $timezone, PDO::PARAM_STR);
$stmt->bindParam(':hashed', $hash, PDO::PARAM_STR);
$stmt->bindParam(':salt', $salt, PDO::PARAM_STR);

$stmt->execute();
  • 写回答

3条回答 默认 最新

  • dongzao4503 2016-02-10 15:23
    关注

    Do it at execute time?

    $stmt->execute(array(':name' => $name, etc....))
    

    Using the formal bindParam() really only makes sense if you're going to be executing the statement in a loop and will be changing the values in $name and the other variables in the loop. If it's a fire/forget single-execution query, might as well just pass the values in the execute() call and skip the formal binding - it's a lot of extra work for basically no benefit.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题