Advertisement
Guest User

Untitled

a guest
Jul 13th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. // These would normally come from an include file...
  2. $db_path = '/var/lib/firebird/2.5/data/MyDatabase.fdb';
  3. $db_user = 'SYSDBA';
  4. $db_pass = 'masterkey';
  5.  
  6. // use php error handling
  7. try {
  8. $dbh = ibase_connect( $db_path, $db_user, $db_pass );
  9. // Failure to connect
  10. if ( !$dbh ) {
  11. throw new Exception( 'Failed to connect to database because: ' . ibase_errmsg(), ibase_errcode() );
  12. }
  13.  
  14. $th = ibase_trans( $dbh, IBASE_READ+IBASE_COMMITTED+IBASE_REC_NO_VERSION);
  15. if ( !$th ) {
  16. throw new Exception( 'Unable to create new transaction because: ' . ibase_errmsg(), ibase_errcode() );
  17. }
  18.  
  19. $qs = 'select FIELD1, FIELD2, from SOMETABLE order by FIELD1';
  20. $qh = ibase_query( $th, $qs );
  21.  
  22. if ( !$qh ) {
  23. throw new Exception( 'Unable to process query because: ' . ibase_errmsg(), ibase_errcode() );
  24. }
  25.  
  26. $rows = array();
  27. while ( $row = ibase_fetch_object( $qh ) ) {
  28. $rows[] = $row->NODE;
  29. }
  30.  
  31. // $rows[] now holds results. If there were any.
  32.  
  33. // Even though nothing was changed the transaction must be
  34. // closed. Commit vs Rollback - question of style, but Commit
  35. // is encouraged. And there shouldn't <gasp>used the S word</gasp>
  36. // be an error for a read-only commit...
  37.  
  38. if ( !ibase_commit( $th ) ) {
  39. throw new Exception( 'Unable to commit transaction because: ' . ibase_errmsg(), ibase_errcode() );
  40. }
  41.  
  42. // Good form would dictate error traps for these next two...
  43. // ...but these are the least likely to break...
  44. // and my fingers are getting tired.
  45. // Release PHP memory for the result set, and formally
  46. // close the database connection.
  47. ibase_free_result( $qh );
  48. ibase_close( $dbh );
  49. } catch ( Exception $e ) {
  50. echo "Caught exception: $en";
  51. }
  52.  
  53. // do whatever you need to do with rows[] here...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement