Location: PHPKode > projects > chillyCMS > chillyCMS/core/database.class.php
<?php
//#################################################################################################
//	Database class
//#################################################################################################
//	chillyCMS - Content Management System
//	Copyright (C) 2008
//	Stefanie Wiegand <hide@address.com> & Johannes Cox <hide@address.com>
//	
//	This program is licensed under the GPL 3.0 license. For more information see LICENSE.txt.
//#################################################################################################
defined('DOIT') or die('Restricted access');

if (file_exists('config.php')) {
	require_once('config.php');
} elseif (file_exists('../config.php')) {
	require_once('../config.php');
} elseif (file_exists('../../config.php')) {
	require_once('../../config.php');
}

//just in case we have not yet installed:
if(!defined('DB_HOST')) { define('DB_HOST',false); }
if(!defined('DB_USER')) { define('DB_USER',false); }
if(!defined('DB_PW')) { define('DB_PW',false); }
if(!defined('DB_DB')) { define('DB_DB',false); }

class Database {
	//Class variables//////////////////////////////////////////////////////////////////////////
	private $host;			//db server address
	private $username;		//db username
	private $password;		//db password
	public	$database;		//db name
	public  $lastquery;		//last executed query
	public  $connection;		//actual connection to db
	public  $error;			//errorstring
	//Functions////////////////////////////////////////////////////////////////////////////////
	//Constructor
	function __construct($host=false,$username=false,$password=false,$database=false) {
		if (!$host)	{ $this->host = DB_HOST; 	} else { $this->host = $host; }
		if (!$username) { $this->username = DB_USER;	} else { $this->username = $username; }
		if (!$password) { $this->password = DB_PW;	} else { $this->password = $password; }
		if (!$database) { $this->database = DB_DB;	} else { $this->database = $database; }
		$this->lastquery	= false;
		$this->connection	= false;
		$this->error		= false;
		$this->connect();
	}
	//Destructor
	function __destruct() { $this->close(); }
	//connect to the database
	private function connect(){
		//try to connect to the db Server
		$this->connection = @mysql_connect($this->host, $this->username, $this->password);
		if($this->connection == false){ $this->error = "Cannot connect to server '".$this->host."'"; }
		//try to select the db
		$dbconnect = @mysql_select_db($this->database, $this->connection);
		if($dbconnect == false){ $this->error = "Cannot connect to database '".$this->database."'"; }
	}
	//do a query
	public function query($sql){
		$return = false;
		//echo "<em>Class DB:</em> $sql<br>";
		if (!is_resource($this->connection)) { $this->__construct(); }
		if (is_resource($this->connection)) {
			if ($this->lastquery = mysql_query($sql, $this->connection)) {
				$return = true;
			} else {
				$this->error = mysql_errno($this->connection).": ".mysql_error($this->connection);
			}
		} else {
			$this->error = "This database connection is no resource.";
		}
		return $return;
	}
	//get a whole table
	public function get_table($table,$order=1) {
		$table=$this->escape($table);
		$order=$this->escape($order);
		if ($this->query("select * from $table order by $order")) { return true; }
		else { return false; }
	}
	//escape a given string or array
	public function escape($string) {
		if (is_array($string) && !empty($string)) {
			foreach ($string as $key=>$value) {
				$string[$key] = $this->escape($value);
			}
			return $string;
		} else {
			if(get_magic_quotes_gpc()) { $string = stripslashes($string); }
			if (!is_resource($this->connection)) { $this->__construct(); }
			return @mysql_real_escape_string($string,$this->connection);
		}
	}
	//get query result in a form depending on the number of returned rows
	public function getdata(){
		$resultCount = @mysql_num_rows($this->lastquery);
		$result = false;
		//if only one line is returned save it in a var
		if ($resultCount==1) {
			$result = @mysql_fetch_assoc($this->lastquery);
		//if there are more lines save them in an array
		} else if ($resultCount > 1) {
			while($row = @mysql_fetch_assoc($this->lastquery)) {
				$result[]=$row;
			}
		} else {
			$result = true;
		}
		return $result;
	}
	//get query result as array
	public function getdata_array() {
		$result=array();
		while($row = @mysql_fetch_assoc($this->lastquery)) { $result[]=$row; }
		return $result;
	}
	//get result count
	public function query_count() {
		return @mysql_num_rows($this->lastquery);
	}
	//find out next autoincrement value
	public function next_autoincrement($table) {
		$table = $this->escape($table);
		if ($this->query("show table status like '$table'")) {
			$result=$this->getdata();
			$result=$result["Auto_increment"];
		} else { $result=false; }
		return $result;
	}
	//close the database connection
	public function close() {
		if (is_resource($this->lastquery)) { @mysql_free_result($this->lastquery); }
		if (is_resource($this->connection)) { @mysql_close($this->connection); }
		$this->lastquery=$this->connection=$this->host=$this->username=$this->password=$this->database=false;
		unset($this);
	}
} ?>
Return current item: chillyCMS