Return to Snippet

Revision: 50728
at August 31, 2011 09:27 by Jotape


Updated Code
<?php

    define('VM_FETCH_ARRAY' , 'array');
    define('VM_FETCH_ASSOC' , 'assoc');
    define('VM_FETCH_OBJECT', 'object');
    define('VM_FETCH_ROW' , 'row');
	
	class Conexao {
        
		private $host		= DB_SERVER;
		private $dbname		= DB_NAME;
		private $user 		= DB_USER;
		private $pswd 		= DB_PASS;
		private $port 		= DB_PORT;
		
		var $_query_id;
		
		private $con;
	
		public function __construct() {
		
		$this->con = pg_connect("host=$this->host dbname=$this->dbname user=$this->user password=$this->pswd port=$this->port");
		if (!$this->con) {
		    die('Não foi possível conectar-se com o servidor: ' . pg_last_error());
			}
		
		} //método construtor
		
		public function fecha(){
			# fecha cpnexão
			pg_close($this->con);
		}
		
		public function execute($sql) {
		$sql = trim($sql);
		$retorno = false;
			
		# executa a query
		$result = pg_query($sql);
			
		//print_r($sql);
		# no caso de select, os dados são colocados em um array
		if (is_bool($result))
			$retorno = $result;	
		else {
			
			while ($row = $this->fetch_assoc($result)) {
				# codifica resultado para utf8
				array_walk($row, 'toUtf8');
   				$retorno[] = $row;
			}
		}
		
		return (is_bool($retorno) && !$retorno)?true:$retorno;
	}
	
		/**
		 * Metodo que irá criar um Array com os dados da consulta.
		 * @param {string} $result: Resultado da Consulta.
		 * @return {array} $rows: Array com os dados da Consulta. 
		 **/
		function criarArray($result = ''){
			$result = !$result ? $this->_queryId : $result;
			$rows = array();
			
			// LOOP que irá criar o Array com os dados.
			while ($row = $this->fetch_object()) {
				# codifica resultado para utf8
				array_walk($row, 'toUtf8');
				$rows[] = $row;
			}
			return $rows;
		}

        public function sqlQuery($sql) {
            $this->_query_id = pg_query($sql) or die(pg_last_error());
            return $this->_query_id ? $this->_query_id : false;
        }
		
        public function insert_id() {
            return pg_insert($this->con);
        }
 
        public function fetch_array($query_id = '') {
            $query_id = (!$query_id) ? $this->_query_id :
            $query_id;
            return pg_fetch_array($query_id, PGSQL_ASSOC);
        }
       
        public function fetch_row($query_id = '') {
            $query_id = (!$query_id) ? $this->_query_id :
            $query_id;
            return pg_fetch_row($query_id);
        }
             
        public function fetch_object($query_id = '') {
            $query_id = (!$query_id) ? $this->_query_id :
            $query_id;
            return pg_fetch_object($query_id);
        }
        
        public function fetch_assoc($query_id = '') {
            $query_id = (!$query_id) ? $this->_query_id :
            $query_id;
            return pg_fetch_assoc($query_id);
        }
               
        public function num_rows($query_id = '') {
            $query_id = (!$query_id) ? $this->_query_id :
            $query_id;
            return pg_num_rows($query_id);
        }

        public function result($result = '', $row = 0, $cols = 0) {
            $result = !$result ? $this->_query_id :
            $result;
            return pg_fetch_result($result, $row, $cols);
        }

        public function num_fields($result = '') {
            $result = !$result ? $this->_query_id :
            $result;
            return pg_num_fields($result);
        }

        public function affected_rows() {
            return pg_affected_rows($this->con);
        }

        public function field_name($result = '', $i = 0) {
            $result = !$result ? $this->_query_id :
            $result;
            return pg_field_name($result, $i);
        }

        public function field_type($result = '', $i = 0) {
            $result = !$result ? $this->_query_id :
            $result;
            return pg_field_type($result, $i);
        }

        function start_transaction() {
            pg_query("START TRANSACTION");
        }
        
        function commit() {
            pg_query("COMMIT");
        }
        
        function rollback() {
            pg_query("ROLLBACK");
        }

    }
?>

Revision: 50727
at August 31, 2011 09:27 by Jotape


Initial Code
<?php

    define('VM_FETCH_ARRAY' , 'array');
    define('VM_FETCH_ASSOC' , 'assoc');
    define('VM_FETCH_OBJECT', 'object');
    define('VM_FETCH_ROW' , 'row');
	
	class Conexao {
        
		private $host		= DB_SERVER;
		private $dbname		= DB_NAME;
		private $user 		= DB_USER;
		private $pswd 		= DB_PASS;
		private $port 		= DB_PORT;
		
		var $_query_id;
		
		private $con;
	
		public function __construct() {
		
		$this->con = pg_connect("host=$this->host dbname=$this->dbname user=$this->user password=$this->pswd port=$this->port");
		if (!$this->con) {
		    die('Não foi possível conectar-se com o servidor: ' . pg_last_error());
			}
		
		} //método construtor
		
		public function fecha(){
			# fecha cpnexão
			pg_close($this->con);
		}
		
		public function execute($sql) {
		$sql = trim($sql);
		$retorno = false;
			
		# executa a query
		$result = pg_query($sql);
			
		//print_r($sql);
		# no caso de select, os dados são colocados em um array
		if (is_bool($result))
			$retorno = $result;	
		else {
			
			while ($row = $this->fetch_assoc($result)) {
				# codifica resultado para utf8
				array_walk($row, 'toUtf8');
   				$retorno[] = $row;
			}
		}
		
		return (is_bool($retorno) && !$retorno)?true:$retorno;
	}
	
		/**
		 * Metodo que irá criar um Array com os dados da consulta.
		 * @param {string} $result: Resultado da Consulta.
		 * @return {array} $rows: Array com os dados da Consulta. 
		 **/
		function criarArray($result = ''){
			$result = !$result ? $this->_queryId : $result;
			$rows = array();
			
			// LOOP que irá criar o Array com os dados.
			while ($row = $this->fetch_object()) {
				# codifica resultado para utf8
				array_walk($row, 'toUtf8');
				$rows[] = $row;
			}
			return $rows;
		}

        public function sqlQuery($sql) {
            $this->_query_id = pg_query($sql) or die(pg_last_error());
            return $this->_query_id ? $this->_query_id : false;
        }
		
        public function insert_id() {
            return pg_insert($this->con);
        }
 
        public function fetch_array($query_id = '') {
            $query_id = (!$query_id) ? $this->_query_id :
            $query_id;
            return pg_fetch_array($query_id, PGSQL_ASSOC);
        }
       
        public function fetch_row($query_id = '') {
            $query_id = (!$query_id) ? $this->_query_id :
            $query_id;
            return pg_fetch_row($query_id);
        }
             
        public function fetch_object($query_id = '') {
            $query_id = (!$query_id) ? $this->_query_id :
            $query_id;
            return pg_fetch_object($query_id);
        }
        
        public function fetch_assoc($query_id = '') {
            $query_id = (!$query_id) ? $this->_query_id :
            $query_id;
            return pg_fetch_assoc($query_id);
        }
               
        public function num_rows($query_id = '') {
            $query_id = (!$query_id) ? $this->_query_id :
            $query_id;
            return pg_num_rows($query_id);
        }

        public function result($result = '', $row = 0, $cols = 0) {
            $result = !$result ? $this->_query_id :
            $result;
            return pg_fetch_result($result, $row, $cols);
        }

        public function num_fields($result = '') {
            $result = !$result ? $this->_query_id :
            $result;
            return pg_num_fields($result);
        }

        public function affected_rows() {
            return pg_affected_rows($this->con);
        }

        public function field_name($result = '', $i = 0) {
            $result = !$result ? $this->_query_id :
            $result;
            return pg_field_name($result, $i);
        }

        public function field_type($result = '', $i = 0) {
            $result = !$result ? $this->_query_id :
            $result;
            return pg_field_type($result, $i);
        }

        function start_transaction() {
            pg_query("START TRANSACTION");
        }
        
        function commit() {
            pg_query("COMMIT");
        }
        
        function rollback() {
            pg_query("ROLLBACK");
        }

    }
?>

Initial URL

                                

Initial Description

                                

Initial Title
Postgre Connect class

Initial Tags
php

Initial Language
PHP