<?php 
    class connect{
        protected $db;
        public function __construct($db, $user, $password){
            try { 
                $this->db = new PDO("mysql:host=localhost;dbname=".$db, $user, $password); 
				$this->db->exec("SET CHARACTER SET utf8");
                $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
                $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
            }catch(PDOException $e) { 
                throw new Exception($e->getMessage());
            }
        }
        public function Disconnect(){
            $this->db = null;
        }
		public function lastID(){
            return  $this->db->lastInsertId();
        }
        public function getRow($query, $params=array()){
            try{ 
                $stmt = $this->db->prepare($query); 
                $stmt->execute($params);
                return $stmt->fetch();  
            }catch(PDOException $e){
					throw new Exception($e->getMessage());
			}
        }
		public function getRows($query, $params=array()){
            try{ 
                $stmt = $this->db->prepare($query); 
                $stmt->execute($params);
                return $stmt->fetchAll();  
            }catch(PDOException $e){
					throw new Exception($e->getMessage());
			}
        }
        public function insertRow($query='', $params = array()){
            try{ 
                $stmt = $this->db->prepare($query); 
                $stmt->execute($params);
            }catch(PDOException $e){
                throw new Exception($e->getMessage());
				return false;
			}
		return true;
        }
       	function prepararCampos($data = array(), $campos){
			$post = array();
			foreach($data as $key=>$fiel){
				if(in_array($key,$campos))
					$post[":$key"]=$fiel;
			}
			return $post;
		}
		function obtenerIp() {
			return $_SERVER['REMOTE_ADDR'];
			if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
				$ip = $_SERVER['HTTP_CLIENT_IP'];
			} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
				$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
			} else {
				$ip = $_SERVER['REMOTE_ADDR'];
			}
			return $ip;
		}
    }