Location: PHPKode > projects > O - OpenSource GroupWare > osgw/osgw/classes/class.db_mysql.php
<?php
/**************************************************************************
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   @Authors:	Ryan Thompson(hide@address.com)
***************************************************************************/
/*$Id: class.db_mysql.php,v 1.31 2004/05/31 03:55:13 rthomp Exp $*/
class db
{
	var $host = '';
	var $user = '';
	var $password = '';
	var $database = '';
	var $conn;
	var $db;
	var $db_type = 'mysql';
	var $query = '';
	var $sql;
	var $insert_id;
	var $num_rows = 0;
	var $row;
	var $record;
	var $error;
	var $config = FALSE;
	var $query_count;
	var $reference;
	

	/**
	@Function: 	db_connect()
 	@Date: 	29-March-2003
 	@Author: 	Ryan Thompson
 	@Description:	Connect to MySQL Database
 	@Variables:
	*/
	function db_connect()
	{			
		if($this->conn = @mysql_connect($this->host, $this->user, $this->password))
		{
			if(@mysql_select_db($this->database, $this->conn))
			{
				return TRUE;
			} else {
				echo mysql_error();
				return FALSE;
			}
		} else {
			echo mysql_error();
			return FALSE;
		}
	}


	/*!
		@function query()
		@author Ryan Thompson
		@abstract Sends query to database
		@version 0.1
		@params $sql - SQL statment to query database
		@return TRUE/FALSE
	*/
	function query($sql)
	{
		GLOBAL $error;
		if($sql == '')
		{
			return FALSE;
		}
		$this->sql = $sql;
		
		$this->query_count++;
		if($this->query[$this->query_count] = @mysql_query($sql, $this->conn))
		{
			$this->reference = $this->query_count;
			$this->num_rows = @mysql_num_rows($this->query[$this->reference]);

		 	$this->error = @mysql_error();
			$this->insert_id = @mysql_insert_id();
			//echo $this->query_count;
			return $this->reference;
		} else {
			
			echo $this->sql;
			echo $this->error = @mysql_error();
			
			$debug_data = array($sql, $this->error);
			$error->debug('SQL', $debug_data);
			return FALSE;
		}
	}
	
	/*!
		@function fetch_results()
		@author Ryan Thompson
		@abstract Fetchs results from query
		@version 0.1
		@return $data - array containing the current record
	*/
	function fetch_results($ref_id = NULL)
	{
		if(isset($ref_id))
		{
			$this->reference = $ref_id;
		}

		$this->record = @mysql_fetch_array($this->query[$this->reference]);


		$this->row++;
		return is_array($this->record);
	}
	
	/*!
		@function db_close()
		@author Ryan Thompson
		@abstract Close connection to dabase
		@version 0.1
		@return TRUE/FALSE
	*/
	function db_close()
	{
		mysql_close($this->conn);
		return TRUE;
	}
	
	
	/*!
		@function create_table()
		@author Ryan Thompson
		@abstract Creates SQL statement for a new table
		@version 0.1
		@params $table_name
		@params $fields - Array containing the fields and their information
		@return $sql
	*/
	function create_table($table_name, $fields)
	{
		
	
		$sql = "CREATE TABLE $table_name (\n";
		
	
	foreach($fields as $key=>$value)
	{
		$sql .= $key;
		
		foreach($value as $field=>$data)
		{
			switch($field)
			{
				case 'type':
					$sql .= " $data";
					$type = $data;
				break;
				case 'size':
					if($type != 'BOOL' && $data != '')
					{
						$sql .= "($data)";
					}
				break;
				case 'null':
					$sql .= $data ."\n";
						
					
				break;
				case 'key':
					switch($data)
					{
						case 'PRIMARY KEY':
							if(strlen($primary) > 1)
							{
								$primary .= ", ";
							}
							$primary .= $key;
						break;
						case 'INDEX':
							if(strlen($index) > 1)
							{
								$index .= ", ";
							}
							$index .= $key;
						break;
						case 'UNIQUE':
							if(strlen($unique) > 1)
							{
								$unique .= ", ";
							}
							$unique .= $key;
						break;
						
					}
				break;
				case 'extra':
			
					$extra = $data;	
					
					
				break;
				case 'default':
				break;
				
			}
			
		
		}
		if(isset($extra))
		{
			$sql .=  $extra;	
			unset($extra);
		}
		$sql .= ", ";
		
	}
	if(isset($primary))
	{	
		$sql .= " PRIMARY KEY ($primary)";
		if(isset($index) || isset($unique))
		{
			$sql .= ", ";	
		}
	}
	if(isset($index))
	{
		$sql .= " INDEX ($index)";
		if(isset($unique))
		{
			$sql .= ", ";	
		}
	}
	if(isset($unique))
	{
		$sql .= " UNIQUE ($unique)";
	}
	$sql .= ")";
			
		unset($primary);
		unset($index);
		unset($extra);
		unset($unique);
		
		return $sql;
		
	}

	/*!
		@function tables()
		@author Ryan Thompson
		@abstract Gets list of existing tables
		@version 0.1
		@return $table_list
		@since 11-10-2003
	*/
	function tables()
	{
		$sql = "SHOW TABLES FROM {$this->database}";
		$this->query($sql);
		
		
		while($this->fetch_results())
		{
			$table_list[] = $this->record[0];
		}
		if(!is_array($table_list))
		{
			$table_list = array();
		}
		return $table_list;
		
	}

	/*!
		@function drop_table()
		@author Ryan Thompson
		@abstract Drops an existing table
		@version 0.1
		@return TRUE
	*/
	function drop_table($table)
	{
		$this->query("DROP TABLE $table");
		return TRUE;
	}

}


?>
Return current item: O - OpenSource GroupWare