Location: PHPKode > projects > Write2Left > Database/MySQL.php
<?php
/*
+------------------------------------------------------
| Write2Left
| (c) timdorr
| http://www.write2left.com
| hide@address.com
| See License.txt for license info
|------------------------------------------------------
| Script: MySQL.php
| Description:
|   DB class for MySQL
| Created Aug-03-2002
+------------------------------------------------------
*/

/* Class: dbdriver
 * Description:
 *   Driver for the current database
 */
class database
{
	var $server		= "";
	var $username	= "";
	var $password	= "";
	var $db			= "";
	var $tblprefix	= "";
	var $connected	= 0;
	var $connectid	= 0;
	var $queryid	= 0;
	var $query		= "";
	var $querycount	= 0;
	var $querytimes	= array();
	var $querylist	= array();

	function database( $server = "", $user = "", $pass = "", $db = "", $tblprefix = "" )
	{
		$this->server		= $server;
		$this->username		= $user;
		$this->password		= $pass;
		$this->db			= $db;
		$this->tblprefix	= $tblprefix;
	}

	//================
	// Connect to our server and select the db
	//================
	function connect()
	{
		$this->connectid = @mysql_connect( $this->server, $this->username, $this->password );

		if ( !$this->connectid )
		{
			$this->error( "FATAL ERROR: Cannot find database $this->server as $this->username\n<br><br>\n
					MySQL Error: " . mysql_error() . "\n<br><br>\n
					Please check your settings and server configuration" );
		}

		if ( !@mysql_select_db( $this->db, $this->connectid ) )
        {
            $this->error( "FATAL ERROR: Cannot find database $this->db\n<br><br>\n
					Please check your settings and server configuration" );
        }

		$this->connected = 1;
	}

	//================
	// All done!
	//================
	function close()
	{
		return mysql_close( $this->connectid );
	}

	//================
	// Simple query function
	//================
	function query( $query_string = "", $no_debug = 0 )
	{
		global $CONFIG;
		
		if( $query_string == '' )
			return null;

		if ( $this->connected == 0 )
			$this->connect();

		if ( $CONFIG['db_prefix'] != "w2l_" )
		{
			$query_string = preg_replace( "/nc_(\S+?)([\s\.,]|$)/", $CONFIG['db_prefix']."\\1\\2", $query_string );
		}
		
		$mtime = microtime();
        $mtime = explode( ' ', $mtime );
        $querystarttime = $mtime[1] + $mtime[0];

		$this->queryid = mysql_query( $query_string, $this->connectid );
		
		if ( $this->queryid )
		{
			$mtime = microtime();
		    $mtime = explode( ' ', $mtime );
			$queryendtime = $mtime[1] + $mtime[0];

			$this->querycount++;
			if( $no_debug != 1 )
			{
				$this->querytimes[] = $queryendtime - $querystarttime;
				$this->querylist[]  = $query_string;
			}

			return $this->queryid;
		}
		else
		{
			$this->error( "Could not compete a query to the database!\n<br><br>\n
						   Query: $query_string\n<br><br>\n
						   MySQL error: ".mysql_error()."\n" );
		}
	}

	//================
	// Querys the db and gets one row.
	//================
	function query_fetch( $query_string = "", $no_debug = 0 )
	{
		global $CONFIG;
		
		if( $query_string == '' )
			return null;
		
		if ( $this->connected == 0 )
			$this->connect();
		
		if ( $CONFIG['db_prefix'] != "w2l_" )
		{
			$query_string = preg_replace( "/nc_(\S+?)([\s\.,]|$)/", $CONFIG['db_prefix']."\\1\\2", $query_string );
		}
		
		$mtime = microtime();
        $mtime = explode( ' ', $mtime );
        $querystarttime = $mtime[1] + $mtime[0];
		
		$this->queryid = @mysql_query( $query_string, $this->connectid );
		
		if ( $this->queryid )
		{
			$mtime = microtime();
		    $mtime = explode( ' ', $mtime );
			$queryendtime = $mtime[1] + $mtime[0];

			$this->querycount++;
			if( $no_debug != 1 )
			{
				$this->querytimes[] = $queryendtime - $querystarttime;
				$this->querylist[]  = $query_string;
			}

			if ( mysql_num_rows( $this->queryid ) > 0 )
			{
				return mysql_fetch_array( $this->queryid );
			}
			else
			{
				return array();
			}
		}
		else
		{
			$this->error( "Could not compete a query to the database!\n<br><br>\n
						   Query: $query_string\n<br><br>\n
						   MySQL error: ".mysql_error()."\n" );
		}
	}

	//================
	// Gets one row from the requested query
	//================
	function fetch_array( $queryid = -1 )
	{
		if( $queryid == -1 )
			$queryid = $this->queryid;
		return mysql_fetch_array( $queryid );
	}

	//================
	// Gets one row from the requested query without numeric indices
	//================
	function fetch_assoc( $queryid = -1 )
	{
		if( $queryid == -1 )
			$queryid = $this->queryid;
		return mysql_fetch_assoc( $queryid );
	}

	//================
	// Seeks the result data to the specified row
	//================
	function data_seek( $queryid, $row )
	{
		if( $queryid == -1 )
			$queryid = $this->queryid;
		return mysql_data_seek( $queryid, $row );
	}
	
	//================
	// Returns the number of rows selected
	//================
	function num_rows( $connectid = -1 )
	{
		if( $connectid == -1 )
			$connectid = $this->connectid;
		return mysql_num_rows( $connectid );
	}

	//================
	// Returns the number of rows affected
	//================
	function affected_rows( $connectid = -1 )
	{
		if( $connectid == -1 )
			$connectid = $this->connectid;
		return mysql_affected_rows( $connectid );
	}

	//================
	// Returns the insert id of the last inserted row
	//================
	function insert_id()
	{
		return mysql_insert_id( $this->connectid );
	}

	//================
	// Prints an error.
	//================
	function error( $error_text = "" )
	{
		print $error_text;
		die("");
	}
}
?>
Return current item: Write2Left