Advertisement
Guest User

Untitled

a guest
Aug 14th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.33 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL);
  3.  
  4. $dsn = 'mysql:dbname=movies;host=127.0.0.1';
  5. $user = 'root';
  6. $password = '';
  7.  
  8. // Yhteyden muodostus
  9. try {
  10.     $pdo = new PDO($dsn, $user, $password); // Uusi PDO objekti
  11.  
  12. } catch (PDOException $e) {
  13.  
  14.     echo 'Connection failed: ' . $e->getMessage();
  15. }
  16.  
  17. if(isset($_POST['search']))  {
  18.  
  19.     $searchq = $_POST['search'];
  20.     $data = array();
  21.  
  22.     $sql = "SELECT * FROM `movie-info` WHERE `keywords` LIKE :keyword"; // SQL-kysely
  23.     $stmt = $pdo->prepare($sql);
  24.  
  25.     // Suoritetaan SQL-kysely
  26.     $stmt->execute(array(
  27.       'keyword' => '%' . $_POST['search'] . '%'
  28.       ));
  29.  
  30.     // Ei hakutuloksia
  31.     if($stmt->rowCount() == 0) {
  32.  
  33.         $data['error'] = true;
  34.         $data['message'] = 'There is no search results!';
  35.        
  36.         echo json_encode($data);
  37.         exit();
  38.  
  39.     // Jos tuloksia
  40.     } else {
  41.  
  42.         $i = 0;
  43.  
  44.         $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
  45.  
  46.         foreach($results as $row) {
  47.            
  48.             // Tallennetaan data -taulukkoon, omalla tietokannan id arvollaan
  49.             $output = '<div id="title"><b>Otsikko: </b>'.$row['title'].'</div>';
  50.             $output .= '<div id="description"><b>Kuvaus: </b>'.$row['description'].'</div>';
  51.             echo $output;
  52.         }
  53.        
  54.     } // end of else
  55. } // end of if statement
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement