Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created May 11, 2018 14:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/b4d5a391024b151667fe99f5bd913544 to your computer and use it in GitHub Desktop.
Save tommcfarlin/b4d5a391024b151667fe99f5bd913544 to your computer and use it in GitHub Desktop.
[PHP] Using sprintf and LIKE in SQL Queries in WordPress
<?php
/**
* Queries a third-party API using sprintf and a LIKE clause to retrieve the users
* with a last name like the specified argument.
*
* @param string $lastName An unsanitized version of the user's last name.
*/
public function getUserByLastName(string $lastName)
{
// Sanitize the argument as you see fit here.
$query = sprintf("
SELECT FIRST_NAME, LAST_NAME
FROM USERINFO
WHERE LAST_NAME LIKE '%%%s%%'
", $lastName);
// Make a query to the third-party API here.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment