Location: PHPKode > projects > MJGUEST > mjguest/database/mysql/driver.php
<?php
	@ini_set('zend.ze1_compatibility_mode', 0);
	/*
	+----------------------------------------------------------------
	|
	|	MJGUEST
	|	=============================================================
	|	Copyright (c) 2002-2008 Giacomo "mdsjack" Menni
	|	Terms of agreement and support at www.mdsjack.bo.it
	|
	+----------------------------------------------------------------
	|	[ DATABASE MODULE ]
	|	Database driver (MySQL 4.1+)
	+----------------------------------------------------------------
	*/
	
	class db_driver
	{
		private $id;
		private $query;
		private $lastquestion;
		
		public $driver		= 'MySQLi'; # const
		public $answer		= '';
		public $tot_queries	= 0;
		public $error		= false;
		
		public $tables		= array();
		
		private $questions = array
		(	'truncate'			=> 'TRUNCATE %s'
		,	'optimize'			=> 'OPTIMIZE TABLE %s'
		,	'row_delete'		=> 'DELETE FROM %s WHERE %s=\'%s\''
		,	'row_count'			=> 'SELECT COUNT(*) FROM %s'
		,	'row_add'			=> 'INSERT INTO %s VALUES()'
		,	'settings_load'		=> 'SELECT * FROM [TBL=settings]'
		,	'settings_update'	=> 'UPDATE [TBL=settings] SET adminpass=\'%s\', adminmail=\'%s\', timezone=%d, dateformat=\'%s\', theme=\'%s\', entriesxpage=\'%u\', wordfilter=\'%b\', title=\'%s\', namelen=\'%u\', msglen=\'%u\', emoticons=\'%u\', avatars_maxkilobytes=\'%u\', redirectscreen=\'%b\', notifynewentry=\'%b\', captcha=\'%b\', messenger=\'%b\', entry_approval=\'%b\', mod_account=\'%b\', modpass=\'%s\', modmail=\'%s\', rating=\'%b\', banned=\'%s\', name_admin=\'%s\', name_mod=\'%s\' '
		,	'settings_restore'	=> 'UPDATE [TBL=settings] SET adminpass=DEFAULT, adminmail=DEFAULT, timezone=DEFAULT, dateformat=DEFAULT, theme=DEFAULT, entriesxpage=DEFAULT, wordfilter=DEFAULT, title=DEFAULT, namelen=DEFAULT, msglen=DEFAULT, emoticons=DEFAULT, avatars_maxkilobytes=DEFAULT, redirectscreen=DEFAULT, notifynewentry=DEFAULT, captcha=DEFAULT, messenger=DEFAULT, entry_approval=DEFAULT, mod_account=DEFAULT, modpass=DEFAULT, modmail=DEFAULT, rating=DEFAULT, banned=DEFAULT, name_admin=DEFAULT, name_mod=DEFAULT '
		,	'entry_add'			=> 'INSERT INTO [TBL=entries] VALUES(NULL, \'%s\', \'%b\', \'%s\',\'%s\', %u, \'%s\', \'%s\', \'%s\', \'%s\',\'%s\',\'%s\',\'%s\', \'\', \'%b\', \'%u\')'
		,	'entry_get_set'		=> 'SELECT id, ip, pvt, name, country, datetime, message, dir, email, web, chat, uin, LENGTH(avatar) AS avatar, reply, approved, rating FROM [TBL=entries] %§ ORDER BY id DESC LIMIT %u,%u'
		,	'entry_get_one'		=> 'SELECT pvt, name, country, message, dir, email, web, chat, uin, LENGTH(avatar) AS avatar, reply, rating FROM [TBL=entries] WHERE id=%u'
		,	'entry_edit'		=> 'UPDATE [TBL=entries] SET pvt=\'%b\', name=\'%s\', country=\'%s\', message=\'%s\', dir=\'%s\', email=\'%s\', web=\'%s\', chat=\'%s\', uin=\'%s\', rating=\'%u\' %§ WHERE id=%u'
		,	'get_avatar'		=> 'SELECT avatar FROM [TBL=entries] WHERE id=%u'
		,	'check_flood'		=> 'SELECT MAX(datetime) FROM [TBL=entries] WHERE ip=\'%s\''
		,	'reply_save'		=> 'UPDATE [TBL=entries] SET reply=\'%s\' WHERE id=%u'
		,	'approve'			=> 'UPDATE [TBL=entries] SET approved=\'1\' WHERE id=%u'
		,	'ban_guest'			=> 'UPDATE [TBL=settings] SET banned=\'%s\''
		,	'rating_stats'		=> 'SELECT AVG(rating) AS avg, COUNT(rating) AS tot FROM [TBL=entries] WHERE rating != 0'
		);
		
		function __construct()
		{
			$this->tables = array
			(	'[TBL=entries]'		=> db_flag.'entries'
			,	'[TBL=settings]'	=> db_flag.'settings'
			);
			
			$this->id = new mysqli(db_host, db_user, db_pass, db_name, (int) db_port);
			
			$this->error();
			
			return true;
		}
		
		function __destruct()
		{
			$this->id->close();
		}
		
		function ask($question, $sql = null, $params = array())
		{
			$this->lastquestion = $question;
			
			$this->query(strtr(vsprintf(str_replace('%§', $sql, $this->questions[$this->lastquestion]), $params), $this->tables));
			
			return true;
		}
		
		public function get_field($num = 0)
		{
			$this->answer = @mysqli_fetch_row($this->query);
			
			return ($this->answer ? $this->answer[$num] : $this->error());
		}
		
		public function get_row()
		{
			$this->answer = @mysqli_fetch_assoc($this->query);
			
			return ($this->answer ? true : $this->error());
		}
		
		private function error()
		{
			if($this->error == true) return false;
			
			if(mysqli_connect_errno()):
				echo "
				<h3>Database Error!</h3>
				<strong>Module:</strong> ".db_type."<br />
				<strong>Error:</strong> ".mysqli_connect_error();
				
				$this->error = true;
				return false;
			endif;
			
			$errno = mysqli_errno($this->id);
			
			if ($errno <> 0):
				echo "
				<h3>Database Error!</h3>
				<strong>Module:</strong> ".db_type."<br />
				<strong>Error (#{$errno} - ".$this->lastquestion."):</strong> ".mysqli_error($this->id);
				
				$this->error = true;
				return false;
			endif;
			
			return true;
		}
		
		public function query($sql)
		{
			$this->query = @mysqli_query($this->id, $sql, MYSQLI_STORE_RESULT);# Unbuffered: MYSQLI_USE_RESULT
			
			$this->tot_queries++;
			
			return ($this->query ? true : $this->error());
		}
		
		public function escape($raw_data)
		{
			return $this->id->real_escape_string($raw_data);
		}
		
		public function sql($sqlfile)
		{
			#--dev: multiquery -> connection lost
			
			$sql = explode(';', str_replace(array('mjguest_', "\r\n"), array(db_flag, ' '), file_get_contents($sqlfile)));
			
			foreach ($sql as $query)
				if (!empty($query))
					if ($this->query($query) == false)
						break;
			
			$this->error();
		}
		
		public function free_memory()
		{
			@mysqli_free_result($this->query);
			
			unset($this->query);
		}
	}
?>
Return current item: MJGUEST