home » zplus/freepost.git
Author zPlus <--> 2016-11-29 00:32:27
Committer zPlus <--> 2016-11-29 00:32:27
Commit d6d1873 (patch)
Tree fc41954
Parent(s)

database.php: add simple search function


commits diff: ab6d789..d6d1873
1 file changed, 33 insertions, 0 deletionsdownload


Diffstat
-rw-r--r-- database.php 33

Diff options
View
Side
Whitespace
Context lines
Inter-hunk lines
+33/-0 M   database.php
index acba971..1c762cb
old size: 40K - new size: 41K
@@ -1311,4 +1311,37 @@ class Database
1311 1311
1312 1312 return true;
1313 1313 }
1314 +
1315 + /**
1316 + * Search database
1317 + *
1318 + * TODO: When MySQL will be updated to v5.6, add fulltext search
1319 + * with scores for better results. InnoDB doesn't support
1320 + * fulltext search with older versions.
1321 + */
1322 + function search ($search_query)
1323 + {
1324 + if (is_null ($this->database))
1325 + return false;
1326 +
1327 + $search_query = trim ($search_query);
1328 +
1329 + if (strlen ($search_query) == 0)
1330 + return false;
1331 +
1332 + $search_query = str_ireplace (' ', '%', $search_query);
1333 +
1334 + $query = $this->database->prepare (
1335 + 'SELECT * ' .
1336 + 'FROM `post`' .
1337 + 'WHERE `title` LIKE ? ' .
1338 + 'ORDER BY `created`' .
1339 + 'LIMIT 100');
1340 +
1341 + $query->execute (['%' . $search_query . '%']);
1342 +
1343 + $results = $query->fetchAll (PDO::FETCH_ASSOC);
1344 +
1345 + return $results;
1346 + }
1314 1347 }