0

I'm building a search function for my site and would like to search in the title and content of the articles.

The query would look like SELECT id FROM articles WHERE title LIKE '%<user input>%' OR content LIKE '%<user input>%';

Is there any way to achieve this with PDO? As I've read, the following is not possible.

$pdo = new PDO('pgsql:host=localhost;dbname=mysite', username, password);
$statement = $pdo->prepare('SELECT id FROM articles WHERE title LIKE :userinput OR content LIKE :userinput');
$search = '%' . $_REQUEST[searchterm] . '%';
$statement->bindParam(':userinput', $search);
Ward Segers
  • 519
  • 5
  • 17
  • Possible duplicate of [implement LIKE query in PDO](http://stackoverflow.com/questions/11117134/implement-like-query-in-pdo) – Xorifelse May 20 '17 at 15:07

1 Answers1

0

I think there is no problem binding multiple variables at the same time, I haven't tested it, but this should work (just change the syntax to using colons and no spaces) :

$statement = $pdo->prepare('SELECT id FROM articles WHERE title LIKE :userinput OR content LIKE :userinput');
$search = '%' . $_REQUEST[searchterm] . '%';
$statement->bindParam(':userinput', $search);
George Dimitriadis
  • 1,681
  • 1
  • 18
  • 27