infojunkie243

abrahamsearch

Mar 18th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.20 KB | None | 0 0
  1. <?php
  2.  
  3. ini_set('memory_limit', '-1');
  4.  
  5. function doSearch(){
  6.     $host = 'localhost';
  7.     $db   = 'webcrawler';
  8.     $user = 'webcrawler';
  9.     $pass = 'secret';
  10.     $charset = 'utf8';
  11.    
  12.     $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
  13.    
  14.     $pdo = new PDO($dsn, $user, $pass);
  15.  
  16.     $sql = 'SELECT * FROM document
  17.    WHERE MATCH (`text`, subject, title, description, creator, publisher)
  18.    AGAINST (:search IN NATURAL LANGUAGE MODE);';
  19.    
  20.     $search = isset($_GET['search']) ? $_GET['search'] : '';
  21.  
  22.     if (empty($search)){
  23.     echo "Please enter a search phrase.<br/>";
  24.     return;
  25.     }
  26.  
  27.     $stmt = $pdo->prepare($sql);
  28.     $stmt->execute(['search' => $search]);
  29.    
  30.     $rowsAffected = $stmt->rowCount();
  31.  
  32.     echo "<em>Found $rowsAffected records matching search for '$search'.</em><br/>";
  33.  
  34.     if ($rowsAffected > 10) {
  35.         echo "<em>Showing first 10 results...</em><br/>";
  36.     }
  37.  
  38.     $counter = 1;
  39.     while($doc = $stmt->fetch(PDO::FETCH_OBJ)) {
  40.         echo "URL: $doc->url    Content-Type: $doc->content_type    Crawled Date: $doc->crawl_date<br/>";
  41.         $counter++;
  42.         if ($counter==10) break;
  43.     }
  44. }
  45. ?>
  46.  <?php doSearch(); ?>
  47. </body>
  48. </html>
Add Comment
Please, Sign In to add comment