Home > Backend Development > PHP Tutorial > How to Correctly Use PDO Parameterized Queries with LIKE Statements?

How to Correctly Use PDO Parameterized Queries with LIKE Statements?

Patricia Arquette
Release: 2024-12-18 00:31:23
Original
376 people have browsed it

How to Correctly Use PDO Parameterized Queries with LIKE Statements?

Creating PDO Parameterized Query with LIKE Statement

Your original attempt at creating a PDO parameterized query with a LIKE statement had a small error. Here's the corrected code:

$query = $database->prepare('SELECT * FROM table WHERE column LIKE ?');
$query->execute(array('value%'));

while ($results = $query->fetch()) {
    echo $results['column'];
}
Copy after login

The only difference is in the execute() method. Instead of using a placeholder with a wildcard ("?%"), you should use a placeholder with a literal wildcard (% at the end). This tells PDO to add the literal wildcard to the value, which is what you want for a LIKE statement.

The above is the detailed content of How to Correctly Use PDO Parameterized Queries with LIKE Statements?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template