Advertisement
wanderleihuttel

Untitled

Jun 7th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.71 KB | None | 0 0
  1. <?php
  2.     /* Firebird.php */
  3.     class Firebird {
  4.  
  5.         private $db_address     = "10.0.0.1";
  6.         private $db_user        = "sysdba";
  7.         private $db_password    = "masterkey";
  8.         private $db_name        = "C:/database/bd.fdb";
  9.         private $connection;
  10.         private $sql;
  11.         private $sql_query;
  12.         private $query;
  13.         private $result;
  14.         private $errormsg;
  15.         private $errorcode;
  16.         private $transaction;
  17.        
  18.         //**************************************** __construct() ****************************************
  19.         public function __construct() {}
  20.        
  21.        
  22.         //******************** connect() ********************
  23.         public function connect() {
  24.             $this->connection = ibase_connect($this->db_address.":".$this->db_name, $this->db_user, $this->db_password);
  25.                 if ( !$this->connection ) {
  26.                    $this->errormsg = "Falha ao conectar o banco Firebird.\n\Firebird error number: ". ibase_errcode()."\n\nFirebird msg: ". ibase_errmsg();
  27.                    $this->errorcode = ibase_errcode();
  28.                    exit();
  29.                 } // fim if
  30.         } // end connect
  31.  
  32.  
  33.         //**************************************** query ****************************************
  34.         public function query($sql_query) {
  35.             $this->sql_query = $sql_query;
  36.             //***** Se existir transação passa o parâmetro da transação ao invés da conexão *****
  37.             if($this->transaction){
  38.                 if( @$this->query = ibase_query( $this->transaction, $this->sql_query ) ) {
  39.                     return $this->query;
  40.                 } else {
  41.                     $this->errormsg = "ERRO AO EXECUTAR COMANDO SQL!\n\n$sql_query\n\nError number: ". ibase_errmsg()."\nError msg: ". ibase_errcode();
  42.                     $this->errorcode = ibase_errcode();
  43.                 }
  44.  
  45.             }
  46.             // ***** Se não existir transação passa o apenas o parâmetro da conexão *****
  47.             else {
  48.                 if( @$this->query = ibase_query( $this->connection, $this->sql_query ) ) {
  49.                     return $this->query;
  50.                 } else{
  51.                     $this->errormsg = "ERRO AO EXECUTAR COMANDO SQL!\n\n$sql_query\n\nError number: ". ibase_errmsg()."\nError msg: ". ibase_errcode();
  52.                     $this->errorcode = ibase_errcode();
  53.                 }
  54.             }
  55.         } // end query
  56.  
  57.  
  58.         /**************************************** fetch ****************************************/
  59.         public function fetch($query) {
  60.             $this->query = $query;
  61.             $this->result = ibase_fetch_assoc($this->query);
  62.             return $this->result;
  63.         } // end fetch
  64.  
  65.  
  66.         /**************************************** rowCount ****************************************/
  67.         public function rowCount($sql_query) {
  68.             $this->sql_query = "select count(*) as numrows from (".$sql_query.")";
  69.             if( $this->query = ibase_query($this->connection, $this->sql_query) ) {
  70.                 $this->result = $this->fetch($this->query);
  71.                 return $this->result['NUMROWS'];
  72.             } else {
  73.                 $this->errormsg = "ERRO AO EXECUTAR COMANDO SQL!\n\n$sql_query\n\nError number: ". ibase_errmsg()."\nError msg: ". ibase_errcode();
  74.                 $this->errorcode = ibase_errcode();
  75.             }
  76.         } // end rowCount
  77.  
  78.  
  79.         /**************************************** beginTransaction ****************************************/
  80.         public function beginTransaction(){
  81.              $this->transaction = ibase_trans( IBASE_DEFAULT, $this->connection );
  82.         } // end beginTransaction
  83.  
  84.  
  85.         /**************************************** commit ****************************************/
  86.         public function commit(){
  87.            if($this->transaction){
  88.               ibase_commit($this->transaction);
  89.            }
  90.            return ibase_close($this->connection);
  91.         } //end commit
  92.  
  93.  
  94.         /**************************************** rollback ****************************************/
  95.         public function rollback(){
  96.             if($this->transaction){
  97.                ibase_rollback($this->transaction);
  98.             }
  99.             return ibase_close($this->connection);
  100.         } // end rollback
  101.  
  102.  
  103.        /**************************************** close ****************************************/
  104.        public function close() {
  105.            return ibase_close($this->connection);
  106.        } // end close
  107.  
  108.  
  109.        /**************************************** errorMsg ****************************************/
  110.        public function errorMsg() {
  111.            return $this->error;
  112.        } // end errorMsg
  113.  
  114.  
  115.        /**************************************** errorCode ****************************************/
  116.        public function errorCode() {
  117.            return $this->errorcode;
  118.        } // end errorcode
  119.  
  120.    } // end Firebird
  121. ?>
  122.  
  123.  
  124. <?php
  125. require_once('Firebird.php');
  126.  /* example.php */
  127. // Instancia um novo objeto Firebird
  128. $firebird = new Firebird();
  129.  
  130. // Conecta com o banco Firebird
  131. $firebird->connect();
  132.  
  133. // Inicia uma nova transação
  134. //$firebird->beginTransaction();
  135.  
  136. $sql_query = "select codigoempresa, nomeempresa from empresa order by nomeempresa";
  137.  
  138. // Executa a query
  139. $query = $firebird->query($sql_query);
  140.  
  141. while($row=$firebird->fetch($query)){
  142.    $codigoempresa = $row['CODIGOEMPRESA'];  
  143.    $nomeempresa   = $row['NOMEEMPRESA'];
  144.    echo "Código: $codigoempresa - Nome: $nomeempresa\n";
  145. }
  146.  
  147. /*
  148. if($query){
  149.    $firebird->commit();
  150.    echo "OK";
  151. }
  152. else{
  153.   $firebird->rollback();
  154.   echo $firebird->errorMsg();
  155. }
  156. */
  157.  
  158. // fecha a conexão
  159. $firebird->close();
  160.  
  161.  
  162. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement