Location: PHPKode > scripts > OoMySql > mysql.class.php
<?
/*#############################################
	mysql RDBMS base 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
	----------------------------------------------------------------------------
	public methods:
		INT = MySql->getAffectedRows();
		STRING = MySql->getClientInfo();
		INT = MySql->getErrNo();
		STRING = MySql->getError();
		STRING = MySql->getHostInfo();	
		STRING = MySql->getMySqlInfo();
		INT = MySql->getNumFields();		
		INT = MySql->getNumRows();
		INT = MySql->getProtoInfo();
		STRING = MySql->getServerInfo();
		STRING = MySql->getStat();
		INT = MySql->insertId();
		BOOL = MySql->isConnected();
		RESOURCE IDENTIFIER = MySql->listDBs();
		databaseObj = MySql->selectDB(STRING database);
		VOID = MySql->setDebugMode(BOOL bool);
		RESOURCE IDENTIFIER = MySql->query(STRING query);
		RESOURCE IDENTIFIER = MySql->listTables(STRING database);
	----------------------------------------------------------------------------
	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 MySql	{
	var $connected;
	var $connection;
	var $server;
	var $database;
	var $user;
	var $password;
	var $debug;
	var $result;
	var $error_msg;
	var $databaseObjCollection;
	var $log_events;
	/*	constructor 
		expects: 	STRING server, STRING user, STRING password, BOOL use_log
		returns:		IF LOGIN OK	=>	database object  
						ELSE			=>	false
		throws: 		DATATYPE ERROR 	*/
	function MySql($server,$user,$password,$use_log = false)	{
		$this->server = $server;
		$this->user = $user;
		$this->password = $password;
		$this->log_events = $use_log;
		$this->_connect();
	}
	
	/*	private method to create connection to database
		expects:	VOID
		returns:		IF LOGIN OK	=>	database object  
						ELSE			=>	false	
		throws: 		DB CONNECTION ERROR */
	function _connect()	{
		$this->_setConnection(mysql_connect($this->server,$this->user,$this->password));
		if(!$this->connection)	
			$this->_error("Could not connect to server '$this->server'.","fatal");
		$this->connected = true;
	}
	
	/* 	private  method to close DB link 
		expects:	VOID
		returns:		IF CLOSE OK	=>	true  
						ELSE				=>	false */
	function _close()	{
	 	$this->result = mysql_close($this->connection);
		if(!$this->result)
			$this->_error("Connection to server could not be closed","fatal");
		$this->connected = false;
		return true;
	}
	
	/* 	private  method to store our database connection
		expects:	MySQL link identifier
		returns:		VOID */
	function _setConnection($connection)		{	$this->connection = $connection;		}
	
	/*	private method to create error message and include 
		mysql error number and message
		expects:	STRING message, STRING type
		retruns:		VOID */
	function _error($message,$type)	{
		global $ERROR;
		$this->error_msg = $this->getErrNo()."::".$this->getError()." - ".$message;
		if($this->log_events)
			$ERROR->throwError($this->error_msg,$type);
	}

	/* 	private  method to get our database connection
		expects:	VOID
		returns:		MySQL link identifier */
	function &getConnection()	{	return $this->connection;		}
	
	/*	public method to send an sql query to database server
		expects:	STRING sql_query
		returns:		RESOURCE IDENTIFIER */
	function query($sql_query)		{
		$this->result = mysql_query($sql_query,$this->connection);
		if($this->debug)	{
			print $sql_query."<br>\n";
			$this->_error();
		}
		return $this->result;
	}
	
	/*	public method to switch the debug messages on and off
		expects: 	BOOL bool
		returns: 	VOID	*/
	function setDebugMode($bool)		{	$this->debug = $bool; }

	/*	public method to check for valid database connection 
		expects:	VOID
		returns:		TRUE || FALSE	*/
	function isConnected()	{ return $this->connected;	}
			
	/*	public method to select database
		expects:	STRING database
		returns:		IF SELECT OK	=> 	database object
		else:			ELSE				=>	false 
		throws:		DATATYPE ERROR */
	function &selectDB($database)	{
		if(!isset($this->databaseObjCollection[$database]))
			$this->databaseObjCollection[$database] = new MySqlDB($this->server,$this->user,$this->password, $this->log_events);
		$result = $this->databaseObjCollection[$database]->selectDB($database);
		return ($result)?$this->databaseObjCollection[$database]:false;
	}
	
	/*	public method to create a database
		expects:	STRING database
		returns:		IF CREATE OK	=> 	database object
		else:			ELSE				=>	false 
		throws:		DATATYPE ERROR */
	function &createDB($database)	{
		$query = "CREATE DATABASE $database";
		$this->result = $this->query($query);
		if (!$this->result)
			$this->_error("Database '$database' could not be created.","fatal");
		return $this->selectDB($database);
	}
	
	function dropDB($database)	{
		$query = "DROP DATABASE $database";
		$this->result = $this->query($query);
		if (!$this->result)
			$this->_error("Database '$database' could not be dropped.","fatal");
	}

	/*	public method to get list of databases
		expects: 	VOID
		returns: 	RESOURCE IDENTIFIER	*/
	function listDBs()		{	
		$this->result = mysql_list_dbs($this->connection);		
		if(!$this->result)
			$this->_error("Could not list databases","info");
		return $this->result;
	}
	
	/*	public method to get list of tables in a DB
		expects: 	STRING database
		returns: 	RESOURCE IDENTIFIER	*/
	function listTables($database)		{	
		$this->result = mysql_list_tables($database);	
		if(!$this->result)
			$this->_error("Could not list tables in database '$database'","info");
		return $this->result;
	}
	
	function getNumRows()		{	return mysql_num_rows($this->result);		}
	function getNumFields()		{	return mysql_num_fields($this->result);	}
	function getAffectedRows()	{	return mysql_affected_rows();	}
	function getServerInfo()	{	return mysql_get_server_info($this->connection);	}
	function getClientInfo()	{	return mysql_get_client_info();		}
	function getHostInfo()	{	return mysql_get_host_info($this->connection);	}
	function getProtoInfo()	{	return mysql_get_proto_info($this->connection);	}
	function getMySqlInfo()	{	return mysql_info($this->connection);	}
	function getStat()			{	return mysql_stat($this->connection);	}
	function insertId()			{	return mysql_insert_id($this->connection);		}
	function getError()			{	return mysql_error($this->connection);	}
	function getErrNo()		{	return mysql_errno(); }
}
?>
Return current item: OoMySql