Location: PHPKode > scripts > DBManager > dbmanager/db.class.php
<?php

/**
* class db
* 
* provides access to MYSQL using these methods:
* open() - connects to the database
* query() - runs SQL-query against the connected database
* last_id() - gets last insert id for the connection
* close() - close database connection
* 
* Errors are handled via the errorReporter-class.
* 
* @package database
* @copyright 2002, Jurgen Campforts
*/

include_once "wandelmee/utils/errorReporter.class.php";
include_once "wandelmee/utils/utilities.php";
include_once "wandelmee/database/resultset.class.php";
 
class db {
	var $_host;
	var $_user;
	var $_pass;
	var $_link;
	var $_database;
	var $_errorReporter;
	var $_result;
	var $maxResult;
	
	/**
	 * db::db()
	 * 
	 * Class constructor
	 * 
	 * @param $host			hostname for connection
	 * @param $user			Username
	 * @param $pass			Password
	 * @param $database		Database to be used for queries and transactions
	 * @return 
	 * @version 18/10/2002
	 * @author jcam
	 */
	function db($host,$user,$pass,$database) {
		$this->_errorReporter = new errorReporter();
		$this->_host = $host;
		$this->_user = $user;
		$this->_pass = $pass;
		$this->_database = $database;
	}
	
	/**
	 * db::open()
	 * 
	 * Opens the MySQL-connection
	 * 
	 * @return true if everything is OK
	 * @version 18/10/2002
	 * @author jcam 
	 */
	function open() {
		$this->_link = mysql_connect($this->_host,$this->_user,$this->_pass) or $this->_errorReporter->raiseError(102, $this->_host);
		mysql_select_db($this->_database, $this->_link) or $this->_errorReporter->raiseError(104, $this->_database);
	}
	
	/**
	 * db::query()
	 * 
	 * Queries the selected MYSQL database
	 * 
	 * @param $sql		SQL Statement (string)
	 * @return true als er de gegevens uitkomen
	 * @version 18/10/2002
	 * @author jcam 
	 */
	function query($sql) {
		/* Determine SQL query type. */
		if(eregi("^delete", $sql) || eregi("^insert", $sql) || eregi("^update", $sql)){
			$qtype = 'a';
		}
		elseif(eregi("^select", $sql)){
			$qtype = 'n';
		}
		
		debuglog("mysql_query: $sql\n", $this->_errorReporter);
		/* Run the SQL query, fail if no result. */
		$this->_result = mysql_query($sql, $this->_link) or $this->_errorReporter->raiseError(101, $sql . mysql_error());
		/* Check for affected rows, based on $qtype. */
		if($qtype == 'n'){
			$this->maxResult = mysql_num_rows($this->_result);
			if(mysql_num_rows($this->_result) == 0){
				$this->_errorReporter->raiseError(1301, $sql);
				return false;
			}
		}
		elseif($qtype == 'a'){
			if(mysql_affected_rows($this->_link) <= 0){
				$this->_errorReporter->raiseError(1302, $sql);
				return false;
			}
		}
		return true;
	}
	
	/**
	 * db::selectQuery()
	 * 
	 * Shortcut function voor een select: voert query uit en 
	 * stored het resultaat in een object array met als instance
	 * variabelen de kolomnamen.
	 * 
	 * @param $sql		sql-statement
	 * @return array met alle resultaten van de resultset waarbij een rij opgeslagen zit
	 * in een object, waar de kolomnamen de instance variabelen zijn.
	 * @version 15/11/2002
	 * @author jcam
	 */
	function selectQuery($sql) {
		$bOK = $this->query($sql);
				
		if ($bOK) {
			$result = $this->getResultSet();
			$resultArray = $result->as_obj_array();
			$result->freeResult();
			
			return $resultArray;
		}
		else
			return false;
	}
	
	/**
	 * db::getResultSet()
	 * 
	 * accessor voor het result
	 * 
	 * @return object met de result
	 * @version 15/11/2002
	 * @author jcam 
	 */
	function getResultSet() {
		if ($this->_result) {
			return new resultset($this->_result);
		}
		else
			return null;
	}

	/**
	 * db::last_id()
	 * 
	 * Retrieves the last insert id on the current connection.
	 * 
	 * @return
	 * @version 18/10/2002
	 * @author jcam 
	 */
	function last_id() {
		$id = mysql_insert_id($this->_link);
		return $id;
	}
	
	/**
	 * db::close()
	 * 
	 * Closes the current connection.
	 * 
	 * @return 
	 * @version 18/10/2002
	 * @Author jcam 
	 */
	function close(){
		mysql_close($this->_link);
	}
	
	function numRows() {
		return $this->maxResult;
	}
}


?>
Return current item: DBManager