What's the correct way to use PDO in PHP?
One of here :
$sth->execute(array(':calories' => $calories, ':colour' => $colour));
Another one from here :
/*** reassign the variables again ***/
$data = array('animal_id'=>4, 'animal_name' => 'bruce');
/*** execute the prepared statement ***/
$stmt->execute($data);
My question is: :key
or key
?
Sorry I don't have a PHP environment here.
+2
a source to share
4 answers
I definitely trust php.net more than phpro.org;)
However, in the same php.net page :
An array of insert values (with named parameters) does not need the als colon prefix key value to work.
+1
a source to share
Both are correct and both examples work correctly.
You can read about it in the manual (user comments): http://www.php.net/manual/en/pdostatement.execute.php#71929
0
a source to share
I am using the second method i.e.
$ query = 'insert into user (username, password) values (: username,: password)';
$ param = array ('username' => $ username, 'password' => $ password);
Parameter The $ for this example can sometimes be done as compact('username', 'password')
- I find it convenient.
0
a source to share