<?php
	class Db extends Message
	{
		var $db_name;
		var $user_name;
		var $password;
		var $dbh;

		function __construct($db_user=0,$db_name='app_pos') 
		{
			$this->db_name = 'app_pos';
			$this->user_name = 'app_pos';
			$this->password = '##1Samir##';
		}
		function db_connect() 
		{
			try {
				  $this->dbh = new PDO("mysql:host=localhost;dbname=".$this->db_name, $this->user_name,$this->password);
				  $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );		
				  
				}
				catch(PDOException $e) {
					echo $e;
				}
			return $this->dbh;
		}

		public function db_query($sql)
		{
			return $this->dbh->prepare($sql);
		}
		
		public function get_num_rows($result)
		{
			return $result->rowCount();
		}
		
		public function get_row($result)
		{
			if($this->get_num_rows($result) <= 0)
			{
				return false;
			}
			else
			{
				return $result->fetch(PDO::FETCH_ASSOC);
			}			 	
		}

		public function get_data_ASSOC($result)
		{
			
			if($this->get_num_rows($result) <= 0)
			{
				return false;
			}
			else
			{
				return 	$result->fetchAll(PDO::FETCH_ASSOC);
				//return 	$result->fetchAll();
				
			}
		}

		public function get_data($result)
		{
			
			if($this->get_num_rows($result) <= 0)
			{
				return false;
			}
			else
			{
				//return 	$result->fetchAll(PDO::FETCH_ASSOC);
				return 	$result->fetchAll();
				
			}
		}
		
		function lastInsert()
		{
			return  $this->dbh->lastInsertId();
		}
		function get_lastInsertId($table_name,$unique_id)
		{
			$stmt = $this->dbh->query("SELECT $unique_id from $table_name ORDER BY $unique_id DESC LIMIT 1"); 
			$lastId = $stmt->fetch(PDO::FETCH_NUM); 
			return $lastId[0];
		
		}
		
		public function is_table_empty($table_name) 
		{
			$result = $this->db->query("SELECT id FROM $table_name");
			if($result->num_rows) {
				return false;
			}
			return true;
		}
	
	  
		
		public function db_disconnect()
		{
			$this->dbh=NULL;
		}
	}
?>