<?php
class DbModel
{
    private $host = 'localhost';
    private $user = 'snapyquo_snapmome_azizul';
    private $pass = 'e{{yz6VX,sB7';
    private $database = 'snapyquo_events_management';
    public $conn;

    public function __construct()
    {
        $this->user = 'root';
        $this->pass = '';
        $this->database = 'snapmome_2016';

        $this->conn = new mysqli($this->host, $this->user, $this->pass, $this->database);

        if ($this->conn->connect_errno) {
            echo 'Connection Failed';
        }
    }

    protected function Exists(string $sql)
    {
        $result = $this->conn->query($sql);
        return ($result && $result->num_rows > 0);
    }


    protected function FetchAllAssoc(string $sql)
    {
        $data = [];
        $result = $this->conn->query($sql);

        if ($result && $result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $data[] = $row;
            }
        }

        return $data;
    }

    protected function FetchOneAssoc(string $sql)
    {
        $result = $this->conn->query($sql);

        if ($result && $result->num_rows === 1) {
            return $result->fetch_assoc();
        }

        return null;
    }

    protected function ExecuteStatement(string $sql, bool $insertId = false, bool $affectedRows = false)
    {
        $result = $this->conn->query($sql);

        if ($result === true) {
            if ($insertId) {
                return $this->conn->insert_id;
            }

            if ($affectedRows) {
                return $this->conn->affected_rows;
            }

            return true;
        }

        return false;
    }

}