Last active
August 29, 2015 14:27
-
-
Save terrymun/3c4aee62d7c29273c88b to your computer and use it in GitHub Desktop.
Formulating LIKE queries using prepared statements and named placedholders
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 | |
| // Get username | |
| $username = 'john'; | |
| $id = 1000; | |
| // Using named placeholders | |
| $stmt = $db->prepare("SELECT user, email, country FROM users WHERE user LIKE :username AND id > :id") | |
| // The order of objects in the array does not matter | |
| // Here we prepend and/or append the wildcard symbol '%' to $username | |
| $stmt->execute(array(':username'=>'%'.$username.'%', | |
| ':id'=>$id)); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment