How do I create a PDO parameterized query with a LIKE statement?

How do I create a PDO parameterized query with a LIKE statement?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Parameterizing Wildcards in PDO: The ? vs % Dilemma

You are attempting to write a secure query that filters data using a LIKE operator, but you've hit a common stumbling block regarding how placeholders interact with wildcard characters. Your proposed code attempts to bind the search term directly to the placeholder: $query = $database->prepare('SELECT * FROM table WHERE column LIKE "?%"');. While this approach works for simple exact matches, it breaks down when you need dynamic wildcards or user input that includes special characters.

The fundamental issue lies in how PDO treats placeholders versus string concatenation. When you pass '%value%' as a parameter, PDO sends the literal string %value% to the database driver. The database engine then interprets this single token. However, if your search term is just "apple", and you want to find rows where the column contains "apple" anywhere in the text, you cannot simply append the wildcard to the placeholder variable after binding. You must construct the SQL string so that the wildcards are part of the pattern sent to the database, not the data being bound.

Constructing the Pattern String

To achieve a case-insensitive partial match (assuming SQL_MODE allows it or you use COLLATE utf8mb4_bin), you need to ensure the % signs are outside the placeholder scope. However, there is a critical nuance: you cannot dynamically generate the SQL string based on user input without risking injection if you aren't careful.

The correct pattern for "contains this value" looks like this in standard SQL:

SELECT * FROM table WHERE column LIKE '%value%'

If you try to bind '%apple%' directly as the parameter, many drivers and database configurations will treat the % as a literal character within the bound value unless specifically configured otherwise. More importantly, if your input string contains single quotes (e.g., searching for an error log containing 'error'), a naive concatenation approach fails immediately because of SQL syntax errors, whereas proper escaping is required.

The safest and most robust method with PDO is to construct the full pattern in PHP before binding it to the query, or to handle the wildcards carefully if using named parameters. However, the most common pitfall developers encounter is trying to bind the wildcard as part of the parameter value itself when the driver expects a raw string match.

The Robust Implementation

Here is how you should structure the code to ensure both security and functionality. You must escape single quotes within your input string before wrapping it in wildcards. This prevents syntax errors if the user searches for something like O'Reilly.

$searchTerm = $_POST['search']; // User input

// Escape single quotes to prevent SQL syntax errors when wrapping with wildcards
$escapedTerm = str_replace("'", "''", $searchTerm);

// Construct the pattern: % + escaped_term + %
$pattern = '%' . $escapedTerm . '%';

$query = $database->prepare('SELECT * FROM table WHERE column LIKE ?');
$query->execute([$pattern]);

while ($result = $query->fetch()) {
    echo $result['column'];
}

Trade-offs and Considerations

One significant trade-off here is performance. Using LIKE '%value%' forces the database engine to perform a full table scan (unless there are specific functional indexes, which are rare). If you are searching for prefixes only, such as finding all names starting with "J", you should change the pattern to ?%. In this case, you can bind the value directly:

$pattern = $searchTerm . '%'; // No quotes needed if no single quotes in input
// Or safer: '%' . $escapedTerm . '%' for containment

If you need to search for a suffix, the pattern becomes ?% is incorrect; it should be %?. You must construct the string accordingly.

Using Laravel's Eloquent Syntax

If you are working within the Laravel framework, you can achieve this without writing raw PDO code. The query builder handles the binding and wildcards elegantly. You simply pass the variable to whereLike, which automatically wraps it in % if you specify the operator correctly, or you can use the fluent interface's where method with a custom value.

$results = DB::table('table')
    ->where('column', 'LIKE', '%' . $searchTerm . '%')
    ->get();

Alternatively, Laravel provides a helper method for this exact scenario:

$results = DB::table('table')->whereLike('column', $searchTerm)->get();

This approach abstracts away the need to manually escape quotes or manage placeholder positions, reducing the surface area for injection vulnerabilities. For complex queries where raw PDO offers better control over execution plans, stick to the prepared statement pattern shown earlier. Always remember that the goal of parameterized queries is to separate code from data; manipulating the SQL string structure (adding %) must happen in PHP before the data enters the query context.

Our network