Last active
August 29, 2015 14:27
-
-
Save terrymun/80502de1723196c76636 to your computer and use it in GitHub Desktop.
Position vs named placeholders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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)); | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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