Location: PHPKode > scripts > OoMySql > mysqldb.class.php
<?
/*#############################################
	MySql Database object
	this is definetly a work in progress - the idea behind this
	is to create an easy way to access MySql via objects that
	hide all SQL code from the programmer
	this class should represent a selected mysql database
	and offer an easy interface to select, insert, update and delete
	data.
	Furthermore you can select a table which will return a table object
	to give easy SQL access to that table.
	----------------------------------------------------------------------------
	public methods:
		RESOURCE IDENTIFIER = MySqlDB->del(STRING table, STRING filter);
		RESOURCE IDENTIFIER = MySqlDB->emptyTable(STRING table);
		STRING = MySqlDB->getName();
		RESOURCE IDENTIFIER = MySqlDB->insert(STRING table, STRING||ARRAY cols, STRING vals);
		RESOURCE IDENTIFIER = MySqlDB->listTables();
		RESOURCE IDENTIFIER = MySqlDB->select(STRING what, STRING from, STRING filter, STRING order);
		tableObject = MySqlDB->selectTable(STRING table_name);
		RESOURCE IDENTIFIER = MySqlDB->update(STRING table, STRING||ARRAY cols_vals, STRING filter);
	----------------------------------------------------------------------------
	code broken by Björn Puttmann, hide@address.com
	please feel free to mail any comment or ideas to the above 
	email adress
	version 0.1			
#############################################*/
class MySqlDB	extends MySql	{
	var $table;
	var $tableObjCollection;
	var $connection;
	
	/* 	constructor 
		expects: database name */
	function MySqlDB($server,$user,$password,$use_log = false)	{
		$this->database = $database;
		$this->MySql($server,$user,$password,$use_log);
	}

	/*	private method to split an associative array into key - value pairs
	 	and generate the mysql syntax for insert||update 
		expects: 	ARRAY $data_array, STRING format
		returns:		ARRAY */
	function _parseArray($data_array,$format) {
 		$column_names = "";
		$insert_values = "";
		while(list($col_name,$ins_value) = each($data_array)){
			if($format == "insert") {
				$column_names .= $col_name.",";
				$insert_values .= "'$ins_value',";
    			}
			elseif($format == "update")
				$column_names .= "$col_name = '$ins_value',";
		}
		$column_names = substr($column_names,0,strlen($column_names)-1);
		$insert_values = substr($insert_values,0,strlen($insert_values)-1);
		return array($column_names,$insert_values);
	}
	
	/*	public method to select database
	expects:	STRING database
	returns:		IF SELECT OK	=> 	database object
					ELSE				=>	false */
	function selectDB($database)		{
		$this->database = $database;
		$this->result = mysql_select_db($this->database, $this->connection);
		if(!$this->result)
			$this->_error("could not select database '$database'","fatal");
		return $this->result;
	}
	
	/*	public method to get name of this database 
		expects: 	VOID
		returns: 	STRING name	*/
	function getName()	{	return $this->database;	}
	
	/*	public method to get list of table is this database 
		expects: 	VOID
		returns: 	RESOURCE IDENTIFIER	*/
	function listTables()	{	return MySql::listTables($this->database);	}

	/*	public method to drop this database
		expects: 	VOID
		returns: 	RESOURCE IDENTIFIER	*/
	function drop()	{	return MySql::dropDB($this->database);	}
		
	/*	public method to select data
		expects:	STRING what, STRING from, STRING filter, STRING order
		returns:		RESOURCE IDENTIFIER */
	function select($what,$tables,$filter,$order)	{
		$query = "SELECT $what FROM $tables";
		$query .= ($filter != "")?" WHERE $filter":"";
		$query .= ($order != "")?" ORDER BY".$order:"";
		return $this->query($query);
	}
	
	/*	public method to insert data
		expects:	STRING table, STRING||ARRAY cols, STRING vals
						if cols is an associative array[colname] = value it
						will be parsed in our private _parseArray method
		returns:		RESOURCE IDENTIFIER */
	function insert($table,$cols,$vals)	{
		if(is_array($cols))
			list ($cols,$vals) = $this->_parseArray($cols,"insert");
		$query = "INSERT INTO $table ($cols) VALUES ($vals)";
		return $this->query($query);
	}
	
	/*	public method to update data
		expects:	STRING table, STRING||ARRAY cols_vals, STRING filter
						if cols_vals is an associative array[colname] = value it
						will be parsed in our private _parseArray method
		returns:		RESOURCE IDENTIFIER */
	function update($table,$cols_vals,$filter)	{
		if(is_array($cols_vals))
			list($cols_vals,$dev0) = $this->_parseArray($cols_vals,"update");
		$query = "UPDATE $table SET $cols_vals";
		$query .= ($where != "")?" WHERE $filter":"";
		return $this->query($query);		
	}
	
	/*	public method to delete data
		expects:	STRING table, STRING filter
		returns:		RESOURCE IDENTIFIER */
	function del($table,$filter)	{
		$query = "DELETE FROM $table WHERE $filter";
		return $this->query($query);
	}
	
	/*	public method to empty a table
		expects:	STRING table */
	function emptyTable($table)	{
		$query = "TRUNCATE TABLE $table";
		return $this->query($query);
	}
			
	/*	public method to select a table within the given database
		this is to give easy access to table meta info
		expects:	STRING table_name
		returns:		IF SELECT OK	=> 	table object
		else:			ELSE				=>	false 
		throws:		DATATYPE ERROR */
	function selectTable($table_name)	{
		$this->table = $table_name;
		if(!isset($this->databaseObjCollection[$database_name]))
			$this->databaseObjCollection[$database_name] = new MySqlTable($table_name, &$this->connection);
		return $this->databaseObjCollection[$database_name];
		
	}
}
?>
Return current item: OoMySql