Fork me on GitHub

Gumshoe

A tiny PHP tool for generating SQL wildcard PDO prepared statements for RDBMS.

When utilizing something like enhanced 'for' or 'foreach' loops to iterate through arrays of multiple user submitted inputs prepped for an SQL wildcard database search, the length of the PDO prepared statement must change. This can prove a tedious task. Gumshoe.php is a dynamic tool that automates the process, and generates a prepared statement as well as its final input argument array.

Download

Download package files from Github.

Installation

Include the Gumshoe.php file in your document:

include 'path/to/Gumshoe.php';

You can also autoload the Gumshoe class:

spl_autoload_register(function($class){
	include 'gumshoe_dir/Gumshoe.php'			
});

Usage

Create a new Gumshoe instance. Gumshoe instances require two arguments:

1) The name of the table from which your prepared statement will be retrieving data. This must be a string.
2) The column names in the aforementioned table in which you will match the statement's wildcards. This must be an array type:

$gumshoe = new Gumshoe('myTable',['column1','column2','column3']);

Use the 'search' method to enter the strings you are searching for. The search method takes one array as an argument:

$gumshoe->search(['strings','to','search','for']);

Retrieve the completed prepared statement with the 'statement' method:

$gumshoe->statement();

Retrieve the completed input array for the prepared statement with the 'args' method:

$gumshoe->args();

An Example:

$gumshoe = new Gumshoe('myTable',['myColumn']);
$gumshoe->search(['search','me']);
$gumshoe->statement();  // SELECT * FROM myTable WHERE myColumn LIKE ? OR myColumn LIKE ?
$gumshoe->args();  // [%search%,%me%]

The statement can be prepared and its arguments executed:

$stmt = $this->pdo->prepare($gumshoe->statement());
$stmt->execute($gumshoe->args());

Compatibility

Gumshoe.php is compatible with any server that supports PHP 5.6+ and most relational databases.

License

Licensed under the MIT License.


Copyright © 2017 Li George