1

I've been urging to know what is the difference between using bindValue and execute(array()) thing-y.

Well let's say I have this example of code

$query = $db->prepare("SELECT embedded_page.uid FROM embedded_page WHERE fbp_id = :fbp_id");
$query->bindValue(":fbp_id", $fbp_id, PDO::PARAM_INT);
$query->execute();

What is the difference between this one?

$query = $db->prepare('SELECT `embedded_page`.`uid`, `embedded_page`.`ticket_id`, `embedded_page`.`event_table` FROM `embedded_page` WHERE `fbp_id` = ?');
$query->execute(array($fbp_id));

Regardless of saving a line of code.

A help would be appreciated.

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
Wesley Brian Lachenal
  • 4,381
  • 9
  • 48
  • 81
  • 1
    I don't think there's any difference except that you can't use the optional 3rd parameter in `PDOStatement::bindValue()` in the array as far as I know. – Mike Mar 24 '13 at 03:41
  • They also using different placeholder types for the different binding methods while they are interchangeable. – Your Common Sense Mar 24 '13 at 05:38

1 Answers1

0

It is already self explained with your example, That bindValue validate input data type in form of specially defined PDO::PARAM_INT or similar. While in second example no such condition in place as prepare using ?. (Some internal validation may be done by engine assuming for string, int and float types.). Also in case of more variables as place holders in query first form is easier to understand.

kuldeep.kamboj
  • 2,566
  • 3
  • 26
  • 63
  • What one need to understand from a bunch of *repetitive* code? – Your Common Sense Mar 24 '13 at 05:24
  • If you have case where posted variables and db fields name ae different then it is really well case for using first one think about it. Using `$query->bindValue(":fbp_id", $some_val, PDO::PARAM_INT);$query->bindValue(":anp_id", $other_val, PDO::PARAM_INT);` is more understandable (for combination of fields and form values) then `$query->execute(array($some_val, $other_val ));` – kuldeep.kamboj Mar 24 '13 at 05:29
  • For long list of parameters It is always much easier to map in first form if form variable names is different than db field names. – kuldeep.kamboj Mar 24 '13 at 05:33