Advertisement
bobsona

Untitled

Jul 31st, 2013
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.69 KB | None | 0 0
  1. <?php
  2. define('DB_SERVER', 'localhost');
  3. define('DB_USER', 'root');
  4. define('DB_PASSWORD', '');
  5. define('DB_NAME', '');
  6. if (isset($_GET['term'])){
  7.     $return_arr = array();
  8.  
  9.     try {
  10.         $conn = new PDO("mysql:host=".DB_SERVER.";port=3306;dbname=".DB_NAME, DB_USER, DB_PASSWORD);
  11.         $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  12.        
  13.         $stmt = $conn->prepare('SELECT title FROM news WHERE title LIKE :term');
  14.         $stmt->execute(array('term' => '%'.$_GET['term'].'%'));
  15.        
  16.         while($row = $stmt->fetch()) {
  17.             $return_arr[] =  $row['title'];
  18.         }
  19.  
  20.     } catch(PDOException $e) {
  21.         echo 'ERROR: ' . $e->getMessage();
  22.     }
  23.     echo json_encode($return_arr);
  24. }
  25. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement