dongxuxian1123 2015-08-28 15:16
浏览 76
已采纳

bindValue / bindParam与数组性能

I have always passed an array of my values into execute as it seemed easier to read imo. I was recently working on a script and noticed it was using bindParam and later came to find out how this passes the variable reference (I never knew).

With that said, in a current project I can think of quite a few uses for bindParam instead of my current array usage.

With that said, is there any performance difference between bindValue/bindParam and using an array on execute... especially in the case of repeated loops? I find myself doing a lot of $stmt->execute( array_merge($binding_clause, $binding) ); where I have a few binds that won't change and some that will, in loops of course, where as far as I can tell using bindParam would be perfect for this.

Does adding the type (PDO::PARAM_STR, PDO::PARAM_INT) with the first have any performance over not doing so when using an array (I believe it is string by default with arrays)?

Example difference between the two (these are prepared statements) :

$binding = array(
    'cw_account_id' => $_SESSION['user']['account_id'],
    'cw_date_start' => $date_start,
    'cw_date_end' => $date_end,
    'cw_start' => $b_counter * 500
);  

$stmt->execute($binding);

compared to

$stmt->bindValue(':cw_account_id', $_SESSION['user']['account_id'], PDO::PARAM_INT);
$stmt->bindValue(':cw_date_start', $date_start, PDO::PARAM_INT);
$stmt->bindValue(':cw_date_end', $date_end, PDO::PARAM_INT);
$stmt->bindValue(':cw_start', $b_counter * 500, PDO::PARAM_INT);

$stmt->execute();

Aside from the questions above, would each $stmt->bindValue() here be another trip to the db as compared to one when using the array method?

Aside from readability of code, and how bindParam references values, are there are performance positives/negatives between the two (small and large scale usage... repeated loops included)?

  • 写回答

1条回答 默认 最新

  • doudizhi947129 2015-08-28 15:20
    关注

    Aside from readability of code, and how bindParam references values, are there are performance positives/negatives between the two

    No.

    would each $stmt->bindValue() here be another trip to the db

    No.

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀