Location: PHPKode > projects > OpenEMR Mobile > mobile/db/dbfunc.php
<?php
/*
Copyright: 2010 Capraro Technologies Inc.
License: Licensed under The GPL
Website: http://www.caprarotechnologies.com
Email: hide@address.com
Author: James Scherzi
*/
?>


<?php
define ("DBHOST","localhost:3306");			/* database server hostname */
define ("DBNAME","openemr");			/* database name */
define ("DBUSER","root");		/* database user */
define ("DBPASS","");		/* database password */

define("MYSQL", 0);


/**
 * Query's recordset
 */
class DbRecordset
{
	var $_rSet;
	var $_rConn;
	var $_rIndex=0;//Index of recordset
	var $_rNumRows=0;
	var $_rNumFields=null;
	var $_rResults = array();

	/**
	 * Default constructor
	 */
	function DbRecordset($rs, $conn)
	{
		//echo "RECORDSET: ".$rs;
		$this->_rSet = $rs;
		$this->_rConn = $conn;
		$this->fetchResults();
	}	

	
	/**
	 * Returns the number of rows in the query's recordset
	 */
	function num_rows(){
		return $this->_rNumRows;
		
	}	
	
	/**
	 * Set index to 0
	 */
	function eof(){
		$isEOF = $this->_rIndex >= $this->num_rows();
		
		if($isEOF){$this->free();}
		
		return $isEOF;
	}
	
	/**
	 * Set index to 0
	 */
	function move_first(){
		$this->_rIndex = 0;
	}

	/**
	 * Set index to 0
	 */
	function move_next(){
		$this->_rIndex++;
	}
	
	/**
	 * Returns the index
	 */
	function get_record_index(){
		return $this->_rIndex;		
	}
	
	/**
	 * Returns the value of a field
	 */
	function fields($fname){
		$rsArray = $this->_rResults[$this->_rIndex];
		$result = $rsArray[$fname];

		return $result;
	}
	
	/**
	 * Frees the results
	 */
	function free(){
		//This isn't used anymore
	}
	
	/**
	 * Fetches the results and creates an array
	 */
	function fetchResults(){		
		while($row = mysql_fetch_array($this->_rSet)) {
			array_push($this->_rResults, $row);
			$this->_rNumRows++;
		}
		
		mysql_free_result($this->_rSet);
	}
}


/**
 * Database connector
 */
class DbQuery
{
	var $_qDbConn;
	var $_qString;
	var $_qResult;
	

	/**
	 * Default constructor
	 */
	function DbQuery($qry, $conn)
	{	
		//trigger_error("QUERY: ".$qry, E_USER_NOTICE);
		
		$this->_qDbConn = $conn;
		$this->_qString = $qry;
	}
	
	/**
	 * Returns recordset for executed query
	 */
	function execute(){
		$dbConnection = $this->_qDbConn;
		$errMsg = "ERROR: Query failed. ";
		$rs = mysql_query($this->_qString) or die($errMsg);
		
		$this->_qResult = new DbRecordset($rs, $this->_qDbConn);		
		return $this->_qResult;
	}
}
/**
 * Database connector
 */
class DbConn
{
	var $_dbType;
	var $_dbConn;
	var $_dbQueries = array();
	
	/**
	 * Default constructor
	 */
	function DbConn()
	{
		$this->_dbType = MYSQL;
	}
	
	/**
	 * Sets the database connection type
	 */
	function setType($type){
		$this->_dbType = $type;
	}
	
	/**
	 * Connect to database
	 */
	function connect($dbHost, $dbName, $dbUser = '', $dbPass = '')
	{
		$errMsg = "Unable to select database: [$dbName, $dbHost, $dbUser, $dbPass]";
		$db_select = null;

		//echo "CONNECTING TO: $dbHost, $dbUser, $dbPass<BR>";
		$db_conn = mysql_connect($dbHost, $dbUser, $dbPass, false,65536);
		$db_select = @mysql_select_db($dbName);

		
		
		$this->_dbConn = $db_conn;	
		
		if (!$db_select)
		{
  			die("Unable to select database");
  		}

		return true;
	}	
	
	/**
	 * Adds query to this database connection
	 */
	function addQuery($query){
		$dQuery = new DbQuery($query, $this);
		array_push($this->_dbQueries, $dQuery);
		return $dQuery;		
	}
	
	/**
	 * Closes connection
	 */
	function close(){
		mysql_close($this->_dbConn);
	}
		

}

 class EMRDbConn extends DbConn
 { 	
	/**
	 * Connect to database
	 */
	function connect()
	{
	  	$ret = false;
		$ret = parent::connect(DBHOST,DBNAME,DBUSER,DBPASS);  	 	
		return $ret; 
	}
 }

?>
Return current item: OpenEMR Mobile