<?php

date_default_timezone_set("Asia/Calcutta");

ini_set("display_errors", 0);

class DB {

    private $db_host = "localhost";
    private $db_user = 'echapp6dec_rcuesr';
    private $db_pass = '4^ryr7CY02t&';
    private $db_name = 'echapp6dec_rcuesr_db';

   /*
      private $db_host = "localhost";
      private $db_user = 'root';
      private $db_pass = '';
      private $db_name = 'rcuesr_db';
      */

    private $con = NULL;
    private $db = NULL;
    //public $SiteUrl = "http://localhost/rcues-room/";
    public $SiteUrl = "https://sritechapp.com/rcues-room/";
    public $document_root_path = "";
    public $mail_from_email_id = "customercare@roombook.in";
    public $mail_from_name = "RCUES";
    
    
    
    public $Host = 'smtpout.europe.secureserver.net';
    public $SMTPAuthval = true;
    public $Usernameval = 'site@sritechapp.com';
    public $Passwordval = 'site1234!@#$';
    public $SMTPSecureval = 'ssl';
    public $Portval = '465';
    public $contact_us_mail = "customercare@globalcare.in";




    function __construct() {

        try {
            $db_options = array(
                PDO::ATTR_EMULATE_PREPARES => false                 ///    // important! use actual prepared statements (default: emulate prepared statements)
                , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION   //// fetch associative arrays (default: mixed arrays)
                , PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"  //Set UTF-8 encoding
            );
            $this->con = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user, $this->db_pass, $db_options);
        } catch (PDOException $e) {
            echo "Connection Failed :: " . $e->getMessage();
            return false;
        }
    }

    function __destruct() {

        $this->con = null;
        $this->db_name = null;
    }

    function ConnectionClose() {
        $this->con = null;
        $this->db_name = null;
    }

    function count($table, $where = 1) {

        $result = array();
        $query = "SELECT * FROM $table WHERE $where";

        $stmt = $this->con->prepare($query);
        $stmt->execute();
        return $stmt->rowCount();
    }

    function insert($data, $table) {
        $columns = "";
        $values = "";

        foreach (array_keys($data) as $column) {
            $columns .= ($columns == "") ? "" : ", ";
            $columns .= $column;

            $values .= ($values == "") ? "" : ", ";
            $values .= ":$column";
        }

        $query = "insert into $table ($columns) values ($values)";
        $stmt = $this->con->prepare($query);
        /* foreach ($data as $dataKey=>$dataVal){
          $stmt->bindValue(":$dataKey", $dataVal);
          } */
        $stmt->execute($data);
        return $this->con->lastInsertId();
    }

    function select($table, $where = 1) {
        $result = array();
        $query = "SELECT * FROM $table WHERE $where";
        $stmt = $this->con->prepare($query);
        $stmt->execute();
        if ($stmt->rowCount() > 0) {
            $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        }
        return $result;
    }

    function selectWithCols($table, $cols = '*', $where = 1) {
        $result = array();
        $query = "SELECT $cols FROM $table WHERE $where";

        $stmt = $this->con->prepare($query);
        $stmt->execute();
        if ($stmt->rowCount() > 0) {
            $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        }
        return $result;
    }

    function update($data, $table, $where) {
        $sql = "";
        $i = 0;
        $count = count($data);
        foreach (array_keys($data) as $column) {
            $sql .= $column . "= :" . $column;
            if ($i < $count - 1)
                $sql .= " , ";
            $i++;
        }
        $query = "UPDATE $table SET $sql WHERE $where";
        $stmt = $this->con->prepare($query);
        /* foreach ($data as $dataKey=>$dataVal){
          $stmt->bindValue(":$dataKey", $dataVal);
          } */
        $stmt->execute($data);
        return TRUE;
    }

    function delete($table, $where) {
        $query = "DELETE FROM $table WHERE $where";
        $stmt = $this->con->prepare($query);
        $stmt->execute();
        return TRUE;
    }

    function getQuery($query) {
        $result = array();
        $stmt = $this->con->prepare($query);
        $stmt->execute();
        if ($stmt->rowCount() > 0) {
            $result = $stmt->fetchAll();
        }
        return $result;
    }

    function ExecuteQuery($query) {
        $stmt = $this->con->prepare($query);
        $stmt->execute();
        return TRUE;
    }
    
     function InsertExecuteQuery($query) {
        $stmt = $this->con->prepare($query);
        $stmt->execute();
        return $this->con->lastInsertId();;
    }

}

?>