Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Firebird.php */
- class Firebird {
- private $db_address = "10.0.0.1";
- private $db_user = "sysdba";
- private $db_password = "masterkey";
- private $db_name = "C:/database/bd.fdb";
- private $connection;
- private $sql;
- private $sql_query;
- private $query;
- private $result;
- private $errormsg;
- private $errorcode;
- private $transaction;
- //**************************************** __construct() ****************************************
- public function __construct() {}
- //******************** connect() ********************
- public function connect() {
- $this->connection = ibase_connect($this->db_address.":".$this->db_name, $this->db_user, $this->db_password);
- if ( !$this->connection ) {
- $this->errormsg = "Falha ao conectar o banco Firebird.\n\Firebird error number: ". ibase_errcode()."\n\nFirebird msg: ". ibase_errmsg();
- $this->errorcode = ibase_errcode();
- exit();
- } // fim if
- } // end connect
- //**************************************** query ****************************************
- public function query($sql_query) {
- $this->sql_query = $sql_query;
- //***** Se existir transação passa o parâmetro da transação ao invés da conexão *****
- if($this->transaction){
- if( @$this->query = ibase_query( $this->transaction, $this->sql_query ) ) {
- return $this->query;
- } else {
- $this->errormsg = "ERRO AO EXECUTAR COMANDO SQL!\n\n$sql_query\n\nError number: ". ibase_errmsg()."\nError msg: ". ibase_errcode();
- $this->errorcode = ibase_errcode();
- }
- }
- // ***** Se não existir transação passa o apenas o parâmetro da conexão *****
- else {
- if( @$this->query = ibase_query( $this->connection, $this->sql_query ) ) {
- return $this->query;
- } else{
- $this->errormsg = "ERRO AO EXECUTAR COMANDO SQL!\n\n$sql_query\n\nError number: ". ibase_errmsg()."\nError msg: ". ibase_errcode();
- $this->errorcode = ibase_errcode();
- }
- }
- } // end query
- /**************************************** fetch ****************************************/
- public function fetch($query) {
- $this->query = $query;
- $this->result = ibase_fetch_assoc($this->query);
- return $this->result;
- } // end fetch
- /**************************************** rowCount ****************************************/
- public function rowCount($sql_query) {
- $this->sql_query = "select count(*) as numrows from (".$sql_query.")";
- if( $this->query = ibase_query($this->connection, $this->sql_query) ) {
- $this->result = $this->fetch($this->query);
- return $this->result['NUMROWS'];
- } else {
- $this->errormsg = "ERRO AO EXECUTAR COMANDO SQL!\n\n$sql_query\n\nError number: ". ibase_errmsg()."\nError msg: ". ibase_errcode();
- $this->errorcode = ibase_errcode();
- }
- } // end rowCount
- /**************************************** beginTransaction ****************************************/
- public function beginTransaction(){
- $this->transaction = ibase_trans( IBASE_DEFAULT, $this->connection );
- } // end beginTransaction
- /**************************************** commit ****************************************/
- public function commit(){
- if($this->transaction){
- ibase_commit($this->transaction);
- }
- return ibase_close($this->connection);
- } //end commit
- /**************************************** rollback ****************************************/
- public function rollback(){
- if($this->transaction){
- ibase_rollback($this->transaction);
- }
- return ibase_close($this->connection);
- } // end rollback
- /**************************************** close ****************************************/
- public function close() {
- return ibase_close($this->connection);
- } // end close
- /**************************************** errorMsg ****************************************/
- public function errorMsg() {
- return $this->error;
- } // end errorMsg
- /**************************************** errorCode ****************************************/
- public function errorCode() {
- return $this->errorcode;
- } // end errorcode
- } // end Firebird
- ?>
- <?php
- require_once('Firebird.php');
- /* example.php */
- // Instancia um novo objeto Firebird
- $firebird = new Firebird();
- // Conecta com o banco Firebird
- $firebird->connect();
- // Inicia uma nova transação
- //$firebird->beginTransaction();
- $sql_query = "select codigoempresa, nomeempresa from empresa order by nomeempresa";
- // Executa a query
- $query = $firebird->query($sql_query);
- while($row=$firebird->fetch($query)){
- $codigoempresa = $row['CODIGOEMPRESA'];
- $nomeempresa = $row['NOMEEMPRESA'];
- echo "Código: $codigoempresa - Nome: $nomeempresa\n";
- }
- /*
- if($query){
- $firebird->commit();
- echo "OK";
- }
- else{
- $firebird->rollback();
- echo $firebird->errorMsg();
- }
- */
- // fecha a conexão
- $firebird->close();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement