Skip to content

Instantly share code, notes, and snippets.

@terrymun
Last active August 29, 2015 14:27
Show Gist options
  • Save terrymun/80502de1723196c76636 to your computer and use it in GitHub Desktop.
Save terrymun/80502de1723196c76636 to your computer and use it in GitHub Desktop.
Position vs named placeholders
<?php
// Assuming that database connection is already open
// Some variables
$userid = 1000;
$country = 'USA';
// Using named placeholders
$stmt = $db->prepare("SELECT user, email, country FROM users WHERE id > :id AND country = :country")
// The order of objects in the array does not matter
$stmt->execute(array(':country'=>$country,
':id'=>$id));
?>
<?php
// Assuming that database connection is already open
// Some variables
$userid = 1000;
$country = 'USA';
// Using positional placeholders
$stmt = $db->prepare("SELECT user, email, country FROM users WHERE id > ? AND country = ?")
// The first array item will correspond to the first ?, hence "id > 1000"
// The second array item will correspond to the second ?, hence "country = 'USA'"
$stmt->execute(array($userid, $country));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment