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


Both are valid, but you are advised to use the: key notation, as this can prevent some errors if you name a variable with a reserved keyword, like ...



+2


a source


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


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


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







All Articles