<?php

class Country {

    // database connection and table name
    private $conn;
    private $table_name = "countries";
    // object properties
    public $id;
    public $name;
    public $description;

    public function __construct($db) {
        $this->conn = $db;
    }

    // used by select drop-down list
    public function readAll() {
        //select all data
        $query = "SELECT
                    Id, Name, Description
                FROM
                    " . $this->table_name . "
                ORDER BY
                    Name";

        $stmt = $this->conn->prepare($query);
        $stmt->execute();

        return $stmt;
    }

    // used by select drop-down list
    public function read() {

        //select all data
        $query = "SELECT
                 Id, Name, Description
            FROM
                " . $this->table_name . "
            ORDER BY
                Name";

        $stmt = $this->conn->prepare($query);
        $stmt->execute();

        return $stmt;
    }


    function readOne() {

        // query to read single record
        $query = "SELECT
                 Id, Name, Description
            FROM
                " . $this->table_name . "
            WHERE
                Id = ?
            LIMIT
                0,1";

        // prepare query statement
        $stmt = $this->conn->prepare($query);

        // bind id of product to be updated
        $stmt->bindParam(1, $this->Id);

        // execute query
        $stmt->execute();

        // get retrieved row
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        // set values to object properties
        $this->Name = $row['Name'];
        $this->Description = $row['Description'];
    }

}

?>