diff --git a/database.php b/database.php index acba971..1c762cb 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; + } }