1

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)?

user756659
  • 3,372
  • 13
  • 55
  • 110
  • 1
    That's not even worth [profiling](http://www.xdebug.org/docs/profiler) (if you cared) in comparison to the database queries themselves, using a suitable schema/indexes, or asynchronous queries. – mario Aug 28 '15 at 15:27

1 Answers1

-2

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 3
    Could you explain your answers? – Rick Smith Aug 28 '15 at 17:08
  • `No.` (In reference to the first, very descriptive part of your answer) - this is incorrect. There are performance negatives/positives between the two (having an array, not having an array). They're just not noticeable on modern hardware. Now, if you tried to compare the two on a cheap Game&Watch-esque toy you got from McDonalds, you might notice a difference. In terms of practicality: it doesn't matter. You're not programming on a Atari 2600 and performing 12 queries; you're at least working on a 90's desktop, so it doesn't matter. – Super Cat Feb 11 '17 at 06:19
  • As far as *practice* goes, it is supposedly better to use the `bind` option as you're explicitly stating the relationship; in my opinion, it doesn't matter. I don't *want* to have to bind 8 times in a row, so the straight `execute` option is what I'd rather you. Use whichever you want. – Super Cat Feb 11 '17 at 06:21