PDO LIKE (original) (raw)

Summary: in this tutorial, you’ll learn how to use PDO to execute a query with the LIKE operator.

Introduction to the SQL LIKE operator #

The LIKE operator returns true if a character string matches a specified pattern. Typically, a pattern includes wildcard characters like:

For example, the %er% will match any string that contains the string er, e.g., peter, understand, etc.

Typically, you use the LIKE operator in the WHERE clause of the SELECT, UPDATE, and DELETE statement.

Execute a query that contains the LIKE operator in PDO #

To execute a query that contains a LIKE operator in PDO, you need to construct the pattern upfront.

For example, to select the book with titles that contain the string ‘es, you first construct a SELECT statement like this:

$sql = 'SELECT book_id, title FROM books WHERE title LIKE :pattern';Code language: PHP (php)

And then bind the string '%es%' to the prepared statement.

The following example illustrates how to execute a query that includes the LIKE operator:

`<?php

/**

}

// connect to the database $pdo = require 'connect.php';

// find books with the title matches 'es' books=findbookbytitle(books = find_book_by_title(books=findbookbytitle(pdo, 'es');

foreach ($books as $book) { echo $book['title'] . '
'; }`Code language: PHP (php)

How it works.

The function find_book_by_title() returns the books with the title that matches with the $keyword.

First, make the pattern by adding the wildcard characters to the beginning and end of the $keyword:

$pattern = '%' . $keyword . '%';Code language: PHP (php)

Second, construct an SQL statement that contains a LIKE operator in the WHERE clause:

$sql = 'SELECT book_id, title FROM books WHERE title LIKE :pattern';Code language: PHP (php)

Third, create a prepared statement:

$statement = <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>p</mi><mi>d</mi><mi>o</mi><mo>−</mo><mo>&gt;</mo><mi>p</mi><mi>r</mi><mi>e</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>e</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">pdo-&gt;prepare(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">p</span><span class="mord mathnormal">d</span><span class="mord mathnormal">o</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">&gt;</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">p</span><span class="mord mathnormal">re</span><span class="mord mathnormal">p</span><span class="mord mathnormal">a</span><span class="mord mathnormal">re</span><span class="mopen">(</span></span></span></span>sql);Code language: PHP (php)

After that, execute the statement with the value that comes from the pattern:

$statement->execute([':pattern' => $pattern]);Code language: PHP (php)

Finally, return all rows from the result set by using the fetchAll() method:

return $statement->fetchAll(PDO::FETCH_ASSOC);Code language: PHP (php)

The following code find books with the title contains the keyword 'es':

`// connect to the database $pdo = require 'connect.php';

// find books with the title matches 'es' books=findbookbytitle(books = find_book_by_title(books=findbookbytitle(pdo, 'es');

foreach ($books as $book) { echo $book['title'] . '
'; }`Code language: PHP (php)

Output:

Marcus Makes a Movie Box of ButterfliesCode language: PHP (php)

Summary #

Did you find this tutorial useful?