Advertisement
Guest User

Untitled

a guest
Jul 19th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.02 KB | None | 0 0
  1. <?php
  2. class DBInterface {
  3.     /************************************************************************************
  4.     *   Author: Bill Nalesnik
  5.     *   Date: 7/2/2008
  6.     *   This class will handle database interactions
  7.     ************************************************************************************/
  8.    
  9.     var $dbuser = 'dbuser';     // default database user name
  10.     var $dbpass = 'dbpass';     // default database password
  11.     var $dbhost = 'dbhost';     // default database host
  12.     var $dbname = 'nrfadmin_NRF';       // default database to connect to
  13.    
  14.     var $dbport = 'dbport';
  15.  
  16.     /***
  17.      * Properties
  18.      */
  19.     var $link;  // link to the database to use anywhere it is needed
  20.     var $result;    // resultset of queries that return one
  21.     var $sql;   // the actual sql of the query to be run
  22.     var $last_id;   // If an 'INSERT' query is run, this contains the id of the last inserted row
  23.     var $rows;  // The number of rows affected or returned by a query
  24.  
  25.     /***
  26.      * This is the constructor function that will allow this to work in PHP 4 and 5
  27.      * @return void
  28.      */
  29.     function DBInterface() {
  30.         //destructor
  31.         register_shutdown_function(array(&$this, '__destruct'));
  32.        
  33.         //constructor
  34.         $argcv = func_get_args();
  35.         call_user_func_array(array(&$this, '__construct'), $argcv);    
  36.     }
  37.    
  38.     /***
  39.      * Constructor - initiates a connection to the database if desired
  40.      * @return void
  41.      * @param $db Object[optional] Name of the database to connect to
  42.      * @param $cn Object[optional] Boolean to determine if a connection should be made
  43.      */
  44.     function __construct($db = '', $cn = true) {
  45.         $this->dbname = ($db == '' ? $this->dbname : $db);
  46.        
  47.         if ($cn === true) {
  48.             $this->db_connect();
  49.         }
  50.     }
  51.    
  52.     /***
  53.      * Actually connect to a database
  54.      * @return boolean true on success, false on failure
  55.      * @param $db Object name of database to connect to
  56.      */
  57.     function db_connect() {
  58.         // In order to minimize the performance bottlenecks, we are going to open a
  59.         //  persistent connection
  60.         $this->link = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpass, true);
  61.        
  62.         if ($this->link) {
  63.             mysql_select_db($this->dbname, $this->link);
  64.             return true;
  65.         }
  66.         else {
  67.             return false;
  68.         }
  69.     }
  70.    
  71.     /***
  72.      * Run a mysql query.  If a result is obtained, then the number of rows is updated
  73.      * @return Boolean true if successful false on falure
  74.      */
  75.     function db_query() {
  76.         $this->result = @mysql_query($this->sql, $this->link);
  77.        
  78.         if ($this->result) {
  79.             $this->rows = mysql_numrows($this->result);
  80.             return true;
  81.         }
  82.         else {
  83.             return false;
  84.         }
  85.     }
  86.    
  87.     /***
  88.      * Runs a mysql action query (INSERT, DELETE, UPDATE...).
  89.      *  If the query is an insert query, this function will also
  90.      *  populate the last_id property with the id number of the last
  91.      *  inserted record.  The rows property of the class is also updated
  92.      * @param extra[optional] Boolean determines if the extra operations are done
  93.      * @return Boolean true if query runs successfully false on falure
  94.      */
  95.     function db_action_query($extra=true) {
  96.         $a = strpos(strtoupper($this->sql), "INSERT");
  97.        
  98.         mysql_query($this->sql, $this->link);
  99.        
  100.         if ($extra) {
  101.             $this->rows = mysql_affected_rows($this->link);
  102.            
  103.             if ($a !== false) {
  104.                 $this->last_id = mysql_insert_id($this->link);
  105.             }
  106.         }
  107.                
  108.         return ($this->rows !== -1 ? true : false);
  109.     }
  110.    
  111.     /***
  112.      * Wrapper function for mysql_fetch_assoc
  113.      * @return Returns an associative array that corresponds to the
  114.      *          fetched row and moves the internal data pointer ahead.
  115.      */
  116.     function get_row() {
  117.         return mysql_fetch_assoc($this->result);
  118.     }
  119.    
  120.     /***
  121.      * Moves the data pointer of a resultset back to the beginning.
  122.      * @return boolean true on success, false on failure
  123.      */
  124.     function rewind() {
  125.         return mysql_data_seek($this->link, 0);
  126.     }
  127.    
  128.     /***
  129.      * Cleanup function which closes the database connection and unsets
  130.      * the resultset variable
  131.      * @return void
  132.      */
  133.     function db_close() {
  134.         @mysql_free_result($this->result);
  135.         @mysql_close($this->link);
  136.     }
  137.    
  138.     function __destruct() {
  139.         $this->db_close();
  140.     }
  141. }  
  142. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement