Skip to content

Instantly share code, notes, and snippets.

@terrymun
Last active August 29, 2015 14:27
Show Gist options
  • Save terrymun/3c4aee62d7c29273c88b to your computer and use it in GitHub Desktop.
Save terrymun/3c4aee62d7c29273c88b to your computer and use it in GitHub Desktop.
Formulating LIKE queries using prepared statements and named placedholders
<?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