tags:

views:

77

answers:

4

One from here:

$sth->execute(array(':calories' => $calories, ':colour' => $colour));

The other 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 the PHP environment here.

+1  A: 

I would definitely trust php.net more than phpro.org ;)

However, in the same php.net page:

An array of insert values (named parameters) don't need the prefixed colon als key-value to work.

maid450
A: 

Both ways are correct and both examples run correct.

You can read about that in manual (user comments): http://www.php.net/manual/en/pdostatement.execute.php#71929

Michał Mech
Have you verified that?
Gtker
Yes, but I prefer http://www.php.net/manual/en/pdostatement.bindparam.php@Macmade wrote important answer, You should read to.
Michał Mech
Can you elaborate what's that error if a variable is named with a reserved keyword?
Gtker
+2  A: 

Both are valid, but you are encouraged to use the :key notation, as it can prevent some errors, if you name a variable with a reserved keyword, for instance...

Macmade
Can you elaborate the errors?
Gtker
Just a (very) dumb example, so you can see what I mean... ; )$q = $pdo->prepare( 'SELECT * FROM table WHERE WHERE' );$q->execute( array( 'WHERE' => '1' ) );
Macmade
He was talking about the array with parameter names and values, not about the query.
binaryLV
I can't figure any case in wich using the `key` form can lead to problems, as you only can use this form in the data array, not in the query, as stated by @binaryLV.$q = $pdo->prepare( 'SELECT * FROM table WHERE WHERE' ); $q->execute( array( 'WHERE' => '1' );will not work.
maid450
A: 

I use the second way, i.e.,

$query = 'insert into user (username, password) values (:username, :password)';
$param = array('username' => $username, 'password' => $password);

$param for this example sometimes can be made as compact('username', 'password') - seems handy to me.

binaryLV
I don't see what you mean by `compact('username', 'password')`
Gtker
He's talking about PHP's [compact](http://php.net/manual/en/function.compact.php) function
Anthony Forloney