crazyoxygen
crazyoxygen

Reputation: 716

Why use bindParam in PDO but don't array()?

I have 2 question

I usually saw this

    $sql->bindParam(':xxx', $yyy, PDO::PARAM_zzz);
    $sql->execute();

Why don't use (1)

$sql->execute(array(':xxx'=>$yyy));

and if there're too many value should I use (2)

    $sql->bindParam(':aaa', $bbb, PDO::PARAM_zzz);
    $sql->bindParam(':ccc', $ddd, PDO::PARAM_zzz);
    $sql->bindParam(':eee', $fff, PDO::PARAM_zzz);
    $sql->execute();

or

    $sql->execute(array(':aaa'=>$bbb,':ccc'=>$ddd,':eee'=>$fff));

Upvotes: 2

Views: 337

Answers (1)

Your Common Sense
Your Common Sense

Reputation: 157895

An answer for the first one is quite simple. An average PHP user is a sure cargo cult programmer, who learns by example, not by understanding. This is why most of php code up to day is of awful quality out of last century practices.

So it goes for the question you asked. A PHP user sees an example with all this long and windy bindParam approach and uses it all the way further. Almost noone bothers to ask himself a question like you did, but just continue to mimic the behavior they learned someday.
As simple as that.

There are a few exceptional cases where you really need separate binding, but indeed too rare to make a habit. Strictly speaking, only one known to me - a LIMIT calause when emulation is turned on.

By the way, personally I find even second approach too wordy, and use it this way (with positional placeholders of course):

$sql->execute(array($bbb,$ddd,$fff));

Upvotes: 6

Related Questions