mercredi 15 juin 2016

How to use the oop in reading from mysql in mysql?

I'm trying to read from mysql with php and with oop.How can use from propeties?

This is my class and search function for reading from the database:

<?php
require_once('dbconfig.php');

class Film {

public $name;
public $year;
public $country;
public $director;
private $conn;

public function __construct() {
    $database = new Database();
    $db = $database->dbConnection();
    $this->conn = $db;
}

public function runQuery($sql) {
    $stmt = $this->conn->prepare($sql); 
    return $stmt;
}
public function search($name,$year,$country,$director) {
    try {
        $stmt = $this->conn->prepare("SELECT * FROM table where name='$name' or year='$year' or country='$country' or director='$director'");
        $stmt->execute();
        $num_rows = $stmt->rowCount();
        if ($num_rows > 0) {

            echo "</br>".$num_rows."&nbsp; film is found. </br>";
            echo "</br><table><tr><th>Name</th><th>Year</th><th>Country</th><th>durationMinutes</th><th>Director</th></tr>";

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

                 echo "<tr><td>" . $row['name'] . "</td><td>" . $row['year'] . "</td><td>" . $row['country'] . "</td><td>" . $row['durationMinutes'] . "</td><td>" . $row['director'] . "</td></tr>";
            }
            echo "</table>";
        } else {
            echo "Nothing found !";
        }
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}
}

I want search() is based on the object and properties.

How to change my code?

Aucun commentaire:

Enregistrer un commentaire