Advertisement
Guest User

Untitled

a guest
Aug 14th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.99 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['submit']) && strlen($_POST['search']) > 1)  {
  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.         echo "<p>Yhteensä " . count($results) . " hakutulosta</p>"; // Tulosta hakutulosten määrä
  47.  
  48.         foreach($results as $row) {
  49.            
  50.             // Tallennetaan data -taulukkoon, omalla tietokannan id arvollaan
  51.             $output = '<div id="title"><b>Otsikko: </b>'.ucfirst($row['title']).'</div>';
  52.             $output .= '<div id="description"><b>Kuvaus: </b>'.ucfirst($row['description']).'</div>';
  53.             $output .= '<div id="imdb"><b>Arvio: </b>'.$row['imdb'].'</div>';
  54.             $output .= '<div id="genre"><b>Genre: </b>'.ucfirst($row['genre']).'</div>';
  55.             $output .= '<div id="actors"><b>Näyttelijät: </b>'.ucfirst($row['actors']).'</div>';
  56.             $output .= '<div id="playtime"><b>Kesto: </b>'.ucfirst($row['playtime']).'</div><br/>';
  57.  
  58.             echo $output; // Tulosta hakutulokset
  59.         }
  60.        
  61.     } // end of else
  62. } else {
  63.  
  64.   // Ei tuloksia
  65.   $data['error'] = true;
  66.   $data['message'] = 'Syötä hakusana';
  67.   echo json_encode($data);
  68. }// end of if statement
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement