Location: PHPKode > scripts > SelectaPix Image Gallery > classes/db.php
<?php
/* +----------------------------------------------------------------------+
   |SelectaPix Open Source Gallery	                                      |
   +----------------------------------------------------------------------+
   | Copyright (c) 2004 OutOfTheTrees			                          |
   |                                                                      |
   | http://www.outofthetrees.co.uk/index.php                             |
   | 												                      |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.0 of the GPL license,       |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.outofthetrees.co.uk/license/2_0.txt.                      |
   | If you did not receive a copy of the SelectaPix license and are 	  |
   | unable to obtain it through the world-wide-web, please send a note   |
   | to hide@address.com so we can mail you a copy immediately.|
   +----------------------------------------------------------------------+ */
if (!defined("ACCESS")) {
    die ("Direct request denied");
}

/* TRUE for debug-mode, FALSE if not */
define("DEBUG", false);

/* DO NOT CHANGE THESES VARIABLES */
define("LOCKED_FOR_READ", "READ");
define("LOCKED_FOR_WRITE", "WRITE");

class mySQL {

	var $connection;
	var $selectedDb;
	var $result;
	var $isConnected;
	var $isLocked;
	var $queryType;

	/* constructor */
	function mySQL() {
		$this->db = SQL_DBASE;
        $this->user  = SQL_USER;
        $this->pass  = SQL_PASS;
        $this->server = SQL_SERVER;
        $this->album_tbl = SQL_ABM_TBL;
        $this->img_tbl = SQL_LNK_TBL;
        $this->connect();
	}

	function connect() {
		if(!defined("DEBUG")) {
			define("DEBUG", false);
		}

		if($this->getConnected()) {
			$this->closeConnection();
		}

		if($this->connection = (mysql_connect($this->server, $this->user, $this->pass))) {
			$this->setConnected(true);
			if($this->db) {
				$this->setDb($this->db);
			}
			return true;
		}
		else {
			$this->setConnected(false);
			$this->printError($this->getMysqlError());
			return false;
		}
	}

	/* destructor */
	function disconnect() {
		if($this->result) {
			$this->freeResult();
		}
		if($this->getLocked()) {
			$this->unlock();
		}
		if($this->getConnected()) {
			$this->closeConnection();
		}
	}

	function setDb($dbName) {
		if(!$this->getConnected()) {
			$this->printError("Not connected in function setDb()");
			return false;
		}
		if($this->selectedDb = mysql_select_db($dbName, $this->connection)) {
			return true;
		}
		return false;
	}

	function getConnected() {
		return $this->isConnected;
	}

	function setConnected($status) {
		$this->isConnected = $status;
	}

	function closeConnection() {
		if($this->getLocked()) {
			$this->unlock();
		}

		if($this->getConnected()) {
			mysql_close($this->connection);
			$this->setConnected(false);
		}
	}

	function freeResult() {
		if($this->result) {
			@mysql_free_result($this->result);
		}
	}

	function query($sql, $return_type='array') {
		if(strlen(trim($sql)) == 0) {
			if(DEBUG==true) {
				$this->printError("No SQL was passed to query()");
			}
			return false;
		}
		if(!$this->getConnected()) {
			if(DEBUG==true) {
				$this->printError($this->getMysqlError());
			}
			return false;
		}
		$queryType = substr(trim($sql), 0, strpos($sql, " "));
		$this->setQueryType($queryType);
		$this->prepare_vars($sql);
		$this->result = @mysql_query($sql, $this->connection);
		if($this->result) {
			switch($return_type) {
				case 'row':
					return $this->fetchRow();
				break;

				case 'obj':
					return $this->fetchObject();
				break;

				case 'index':
					return $this->fetchIndex0();
				break;

				case 'none':
					return true;
				break;

				default:
					return($res_array = $this->result_to_array());
				break;
			}
			return true;
		}
		else {
			if(DEBUG==true) {
				$this->printError($this->getMysqlError());
			}
			return false;
		}
	}

	function setQueryType($type) {
		$this->queryType = strtoupper($type);
	}

	function getQueryType() {
		return $this->queryType;
	}

	function getNumRows() {
		if($this->result) {
			if(DEBUG==true) {
				print("<p style=\"background-color: red;\">".$this->getQueryType()."</p>");
			}
			return @mysql_affected_rows($this->connection);
		}
		return false;
	}

	function getQueryResult() {
		return $this->result;
	}

	function fetchArray() {
		if($this->result) {
			return $this->prepare_vars(@mysql_fetch_array($this->result));
		}
		return false;
	}

	function fetchObject() {
		if($this->result) {
			return @mysql_fetch_object($this->result);
		}
		return false;
	}

	function fetchRow() {
		if($this->result) {
			return $this->prepare_vars(@mysql_fetch_row($this->result));
		}
		return false;
	}

	function fetchIndex0() {
		if($this->result) {
			return $this->prepare_vars(@mysql_result($this->result, 0));
		}
		return false;
	}

	function fetchInsertID() {
		if($this->result) {
			return @mysql_insert_id();
		}
		return false;
	}

	function result_to_array($array=null) {
		if(!empty($array)) {
			$this->result = $array;
		}
		$res_array = array();
	   	for ($count=0; $row = @$this->fetchArray(); $count++) {
	     	$res_array[$count] = $this->prepare_vars($row);
	   	}
	   	return $res_array;
	}

	function formatdate($date) {
	    //Reformat a mySQL date into a more readable one
		list($this->yyyy, $this->mm, $this->dd) = explode('-',$date);
	    $this->date = date('j F Y', mktime(0,0,0,$this->mm,$this->dd,$this->yyyy));
	    return $this->date;
	}

	function unlock() {
		if(!$this->getConnected()) {
			$this->setLocked(FALSE);
		}
		if($this->getLocked()) {
			$this->query("UNLOCK TABLES");
			$this->setLocked(FALSE);
		}
	}

	function lock($command) {
		if($this->query("LOCK TABLE ".$command)) {
			$this->setLocked(TRUE);
			return true;
		}

		$this->setLocked(FALSE);
		return false;
	}

	function setReadLock($table) {
		return $this->lock($table." ".LOCKED_FOR_READ);
	}

	function setWriteLock($table) {
		return $this->lock($table." ".LOCKED_FOR_WRITE);
	}

	function setLocked($status) {
		$this->isLocked = $status;
	}

	function getLocked() {
		return $this->isLocked;
	}

	function printError($text, $killApp=false) {
		if($text) {
			print("<p><strong>Error</strong><br />".$text."</p>");
		}
		if($killApp) {
			exit();
		}
	}

	function getMysqlError() {
		if(mysql_error()) {
			return "<br /><p><strong>Mysql Error Number ".mysql_errno()."</strong></p><p>".mysql_error()."</p>";
		}
		return false;
	}

	function prepare_vars($input) {
		switch($this->getQueryType()) {
			case 'SELECT':
				if (is_array($input)) {
					foreach ($input as $this->key => $this->value) {
				   		$output[$this->key] = trim(stripslashes($this->value));
					}
				}
				else {
					$output = trim(stripslashes($input));
				}
				return $output;
			break;

			case 'UPDATE':
			case 'INSERT':
				if (is_array($input)) {
					foreach ($input as $key => $value) {
				   		$output[$key] = trim(addslashes($value));
					}
				}
				else {
					$output = $input;
				}
				return $output;
			break;
		}
	}
}
?>
Return current item: SelectaPix Image Gallery