From d6d1873fb83086b7f1bbb51f13842f4598531eec Mon Sep 17 00:00:00 2001 From: zPlus <--> Date: Tue, 29 Nov 2016 01:32:27 +0100 Subject: [PATCH] database.php: add simple search function --- database.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/database.php b/database.php index acba971e..1c762cba 100644 --- a/database.php +++ b/database.php @@ -1311,4 +1311,37 @@ class Database return true; } + + /** + * Search database + * + * TODO: When MySQL will be updated to v5.6, add fulltext search + * with scores for better results. InnoDB doesn't support + * fulltext search with older versions. + */ + function search ($search_query) + { + if (is_null ($this->database)) + return false; + + $search_query = trim ($search_query); + + if (strlen ($search_query) == 0) + return false; + + $search_query = str_ireplace (' ', '%', $search_query); + + $query = $this->database->prepare ( + 'SELECT * ' . + 'FROM `post`' . + 'WHERE `title` LIKE ? ' . + 'ORDER BY `created`' . + 'LIMIT 100'); + + $query->execute (['%' . $search_query . '%']); + + $results = $query->fetchAll (PDO::FETCH_ASSOC); + + return $results; + } }