Location: PHPKode > projects > SkullPuppy > Project/library/SP/SQL.php
<?php

class SP_SQL {
	protected $_username, $_password, $_server, $_database, $_db, $_debug;
	public $id, $count, $result, $resource;

	public function __construct() {
		$argc = func_num_args();
		$argv = func_get_args();

		if(!in_array($argc, array(0,1,3,4))){
			throw new SP_Exception("Invalid argument count for SP_SQL");
			return false;
		}
		$username = $password = $server = null;
		$loadAuth = false;

		switch($argc) {
		case 3:
			list($server, $username, $password) = $argv;
			break;
		case 4:
			list($server, $username, $password, $loadAuth) = $argv;
			break;
		case 1:
			$loadAuth = $argv[0];
		case 0:
			$loadAuth = true;
		}

		if($loadAuth) {
			$this->loadAuth();
			$username = $this->_username;
			$password = $this->_password;
			$server = $this->_server;
		}

		$server || $server = "localhost";
		$this->_db = mysql_connect($server, $username, $password);
		$this->_debug = false;
	}

	public function debug() {
		$this->_debug = true;
		return $this;
	}

	public function setDb($db) {
		mysql_select_db($db, $this->_db);
	}

	private function loadAuth() {
		$this->_username = SP_Registry::getKey('db_username');
		$this->_password = SP_Registry::getKey('db_password');
		$this->_server = SP_Registry::getKey('db_server');
		if(SP_Registry::getKey('db_database'))
			$this->_database = SP_Registry::getKey('db_database');
	}

	public function query($sql, $TYPE=MYSQL_ASSOC) {
		$this->setDb($this->_database);
		$this->result = array();
		try {
			if($this->_debug) {
				print "DEBUG: \"$sql\"";
				$this->resource = false;
				$this->id = false;
				$this->count = 0;
				$this->result = array();
				return;
			}else $resource = mysql_query($sql, $this->_db) or $sql_error=true;
		} catch (Exception $e) {
			 throw new SP_Exception($e);
		}
		if($sql_error) throw new SP_Exception(mysql_error());
		$this->resource = $resource;
		if(is_resource($resource))
			while($result = mysql_fetch_array($resource, $TYPE))
				$this->result[] = $result;
		$this->id = mysql_insert_id();
		$this->count = mysql_affected_rows();
	}
}

?>
Return current item: SkullPuppy