Location: PHPKode > projects > AV Book Library > avbooklibrary-v1.3.0/web-files/lib/db.php
<?php
 
/**
  * MySQL/PHP Database access class with NO abstraction layer.
  * @author Sherri Wheeler
  * @version  2.00
  * @copyright Copyright (c) 2009, Sherri Wheeler www.start.ofitall.com
  * This software is available for use ONLY via the licence terms.
  */
 
 
/*********************************************************************
* DB ABSTRACTION OBJECT for MYSQL/PHP
*
* This class provides a basic abstraction of database access.
*
* Recommended to instantiate this in a config or top of page, then use
* global on the object to access it in functions and other objects.
*
***********************************************************************/
 
 
/**
  * DB Class
  */
class DB {
 
	/**
      * The connection object.
      * @var  object         
      */
	var $dbc;
 
	/**
      * The database name.
      * @var  string        
      */
	var $db_name;	
 
	/**
      * The database user.
      * @var  string        
      */
	var $user;
 
	/**
      * The database password.
      * @var  string        
      */
	var $pass;
 
	/**
      * The database host name.
      * @var  string        
      */
	var $host;
 
	/**
      * The most recent error message (For debugging).
      * @var  string        
      */
	var $err_msg;		
 
	/**
      * The most recent query passed to this class.
      * @var  string        
      */
	var $last_query;	
 
 
	/**
	  * Initiates the instance variables
	  *
	  * @return void
	  * @access public
	  */
	function init() {
 
		// Initialize any vars.	
		 $last_query = "";
	}
 
 
	/**
	  * Sets the db variables.
	  *
	  * @return void
	  * @param string $db_name
	  * @param string $user
	  * @param string $pass
	  * @param string $host
	  * @access public
	  */
	function set_db_settings($db_name, $user, $pass, $host){		
 
		$this->db_name 	= $db_name;
		$this->host	= $host;
		$this->user	= $user;
		$this->pass	= $pass;			
	}
 
 
	/**
	  * Connect to the db.
	  *
	  * @return void
	  * @access public
	  */
	function connect() {
 
		// Connect to db.
		if( !$this->dbc = mysql_connect($this->host, $this->user, $this->pass) ){
			$this->err_msg = "Failed connecting to database.";
			return FALSE;
		}else{
			if ( !mysql_select_db($this->db_name, $this->dbc) ) {
				$this->err_msg = "Failed selecting database.";
				return FALSE;
			}
		}
	}
 
 
	/**
	  * Disconnect from the db.
	  *
	  * @return void
	  * @access public
	  */
	function disconnect() {
		mysql_close($this->dbc);
		unset($this->dbc);		
	}
 
 
 
	/**
	  * Run a NON select query on the db.
	  *
	  * @return boolean
	  * @param string $query
	  * @access public
	  */
	function query($query) {
 
 		$last_query = $query;
 
		// Run a query and return a fail/success flag. Set $err_msg if there was a problem.
		$result = mysql_query($query, $this->dbc);
 
		// ERROR CHECKING		
		if($result===FALSE){
			$this->err_msg = mysql_error($this->dbc);
			return FALSE;		
		}else{		
			return TRUE;
		}		
	}
 
 
	/**
	  * Run a multi row select query on the db.
	  *
	  * @return array
	  * @param string $query
	  * @access public
	  */
	function select($query) {
 
		// Run a query and return the results. Set $err_msg if there was a problem.
 
 		$last_query = $query;
 
		$result = mysql_query($query, $this->dbc);
 
		// ERROR CHECKING		
		if($result===FALSE || $result == NULL){
			$this->err_msg = mysql_error($this->dbc);
			return FALSE;		
		}

 		if( mysql_num_rows($result)==0 ){
			return FALSE;
		}

		// Parse into an array
		$resArray = array();		
		while ($row = mysql_fetch_assoc($result)) {
			$resArray[] = $row;
		}		
 
		return $resArray;		
	}
 
 
 
	/**
	  * Run a single row select query on the db.
	  *
	  * @return array
	  * @param string $query
	  * @access public
	  */
	function getRow($query) {
 
		// Run a query and return the results. Set $err_msg if there was a problem.
 
 		$last_query = $query;
 
		$result = mysql_query($query, $this->dbc);
 
		// ERROR CHECKING		
		if($result===FALSE || $result == NULL){
			$this->err_msg = mysql_error($this->dbc);
			return FALSE;		
		}
 
		return mysql_fetch_assoc($result);			
	}
 
 
	/**
	  * Quickly grab one value from the db based on table name and key value.
	  * Error or no result will return NULL;
	  *
	  * @return string
	  * @param string $tblname
	  * @param int $key_value
	  * @param string $col_name
	  * @param string $key_name
	  * @access public
	  */
	function getValue($tblname, $key_value, $col_name, $key_name="id"){			
 
		// ** Important: You must escape $key_value BEFORE calling this function. **
 
		$query = "select ".$col_name." from ".$tblname." WHERE ".$key_name."='".$key_value."' LIMIT 1";
 
 		$last_query = $query;
 
		$result = mysql_query($query, $this->dbc);	
 
		// ERROR CHECKING		
		if($result===FALSE || $result == NULL){
			$this->err_msg = mysql_error($this->dbc);
			return FALSE;		
		}						
 
		return ( mysql_result($result, 0, $col_name) );
	}	
 
 
	/**
	  * Return the id of the last inserted row or FALSE on fail.
	  *
	  * @return int
	  * @access public
	  */
	function insertedId() {			
		return( mysql_insert_id($this->dbc) );
	}
 
 
 
	/**
	  * Return the number of affected rows.
	  *
	  * @return int
	  * @access public
	  */
	function affectedRows() {		
		return( mysql_affected_rows($this->dbc) );
	}
 
 
	/**
	  * Escape the given string for this db connection. Second parameter tells it whether to stripslashes.
	  *
	  * @return string
	  * @access public
	  */
	function escapeStr($inStr, $isGPC=FALSE) {
		if($isGPC){
			$inStr = stripslashes($inStr);
		}
		return mysql_real_escape_string($inStr, $this->dbc);
	}
 
 
} // End Class DB.
?>
Return current item: AV Book Library