Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DBInterface {
- /************************************************************************************
- * Author: Bill Nalesnik
- * Date: 7/2/2008
- * This class will handle database interactions
- ************************************************************************************/
- var $dbuser = 'dbuser'; // default database user name
- var $dbpass = 'dbpass'; // default database password
- var $dbhost = 'dbhost'; // default database host
- var $dbname = 'nrfadmin_NRF'; // default database to connect to
- var $dbport = 'dbport';
- /***
- * Properties
- */
- var $link; // link to the database to use anywhere it is needed
- var $result; // resultset of queries that return one
- var $sql; // the actual sql of the query to be run
- var $last_id; // If an 'INSERT' query is run, this contains the id of the last inserted row
- var $rows; // The number of rows affected or returned by a query
- /***
- * This is the constructor function that will allow this to work in PHP 4 and 5
- * @return void
- */
- function DBInterface() {
- //destructor
- register_shutdown_function(array(&$this, '__destruct'));
- //constructor
- $argcv = func_get_args();
- call_user_func_array(array(&$this, '__construct'), $argcv);
- }
- /***
- * Constructor - initiates a connection to the database if desired
- * @return void
- * @param $db Object[optional] Name of the database to connect to
- * @param $cn Object[optional] Boolean to determine if a connection should be made
- */
- function __construct($db = '', $cn = true) {
- $this->dbname = ($db == '' ? $this->dbname : $db);
- if ($cn === true) {
- $this->db_connect();
- }
- }
- /***
- * Actually connect to a database
- * @return boolean true on success, false on failure
- * @param $db Object name of database to connect to
- */
- function db_connect() {
- // In order to minimize the performance bottlenecks, we are going to open a
- // persistent connection
- $this->link = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpass, true);
- if ($this->link) {
- mysql_select_db($this->dbname, $this->link);
- return true;
- }
- else {
- return false;
- }
- }
- /***
- * Run a mysql query. If a result is obtained, then the number of rows is updated
- * @return Boolean true if successful false on falure
- */
- function db_query() {
- $this->result = @mysql_query($this->sql, $this->link);
- if ($this->result) {
- $this->rows = mysql_numrows($this->result);
- return true;
- }
- else {
- return false;
- }
- }
- /***
- * Runs a mysql action query (INSERT, DELETE, UPDATE...).
- * If the query is an insert query, this function will also
- * populate the last_id property with the id number of the last
- * inserted record. The rows property of the class is also updated
- * @param extra[optional] Boolean determines if the extra operations are done
- * @return Boolean true if query runs successfully false on falure
- */
- function db_action_query($extra=true) {
- $a = strpos(strtoupper($this->sql), "INSERT");
- mysql_query($this->sql, $this->link);
- if ($extra) {
- $this->rows = mysql_affected_rows($this->link);
- if ($a !== false) {
- $this->last_id = mysql_insert_id($this->link);
- }
- }
- return ($this->rows !== -1 ? true : false);
- }
- /***
- * Wrapper function for mysql_fetch_assoc
- * @return Returns an associative array that corresponds to the
- * fetched row and moves the internal data pointer ahead.
- */
- function get_row() {
- return mysql_fetch_assoc($this->result);
- }
- /***
- * Moves the data pointer of a resultset back to the beginning.
- * @return boolean true on success, false on failure
- */
- function rewind() {
- return mysql_data_seek($this->link, 0);
- }
- /***
- * Cleanup function which closes the database connection and unsets
- * the resultset variable
- * @return void
- */
- function db_close() {
- @mysql_free_result($this->result);
- @mysql_close($this->link);
- }
- function __destruct() {
- $this->db_close();
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement