daily pastebin goal
31%
SHARE
TWEET

Untitled

a guest Aug 13th, 2017 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if (isset($_GET['term'])){
  2. $return_arr = array();
  3.  
  4. try {
  5.     $conn = new PDO("mysql:host=".DB_SERVER.";dbname=".DB_NAME, DB_USER, DB_PASSWORD);
  6.     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  7.     $terms = ($_GET['term']);
  8.     function isUTF8($terms) {
  9.         return (utf8_encode(utf8_decode($terms)) == $terms);
  10.     }
  11.     $searchTerms = explode(' ',$terms);
  12.  
  13.     $searchTermBits = array();
  14.     foreach ($searchTerms as $term) {
  15.         $term = trim($term);
  16.         if (!empty($term)) {
  17.             $searchTermBits[] = "docType RLIKE '.$term.' OR docTitle RLIKE '.$term.'";
  18.         }
  19.     }
  20.     $stmt = $conn->prepare('SELECT * FROM openDocs WHERE '.implode(' AND ', $searchTermBits).' LIMIT 50');
  21.     $stmt->execute(array('term' => '%'.$_GET['term'].'%'));
  22.     while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  23.         $return_arr[] =  $row;
  24.     }
  25. } catch(PDOException $e) {
  26.     echo 'ERROR: ' . $e->getMessage();
  27. }
  28.  
  29. echo json_encode($return_arr);}
RAW Paste Data
Top