简体   繁体   中英

Searching by keywords in Slim Framework routes

I have a route setup that should return each record that includes the search term in the column description. Right now if i use the term such as toy, it will only return columns that only have the keyword toy but not the column that has "this toy is great" for example.

//Keyword results
  $app->get('/search-description/[{keyword}]', function ($request, $response, $args) {
   $sth = $this->db->prepare("SELECT * FROM products WHERE description LIKE :keyword ORDER BY name");
   $keyword = "%".$keyword."%";
   $sth->bindParam("keyword", $args['keyword']);
   $sth->execute();
   $keywordresults = $sth->fetchAll();
 return $this->response->withJson($keywordresults);
});

My question is how do I use wildcards in routes?

Thanks so much for your help!

I figured out what I was doing wrong. It wasn't as complicated as I was trying to make it using data from http://php.net/manual/en/pdostatement.bindparam.php .

//Keyword results
$app->get('/search-description/[{keyword}]', function ($request, $response, 
$args) {
$sth = $this->db->prepare("SELECT * FROM products WHERE description LIKE '%' :keyword '%' ORDER BY name");
$sth->bindParam("keyword", $args['keyword']);
$sth->execute();
$keywordresults = $sth->fetchAll();
return $this->response->withJson($keywordresults);
});

In the question

$keyword = "%".$keyword."%";

This variable was directed nowhere and not necessary.

Hope this helps someone else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM