Location: PHPKode > scripts > EZMVC > ezmvc/core/quickdb.class.php
<?php
	#########################################################
	#			QuickDB->MySQL Wrapper Class				#
	#-------------------------------------------------------#
	#			Created By: SARFRAZ AHMED CHANDIO			#
	#					Web Developer						#
	#					Brains Technology					#
	#					http://www.brainstechnology.com		#
	#			Date Created: 12 April 2009					#
	#########################################################
	
	// Things To Do:
	//		Paging
	//		Multi-Language Support
	/////////////////////////////////////////////////////////


	class QuickDB
	{
		private $con;
		private $result;
		private $row;
		private $affected;
		
		// Intialize the class with connection to db
		public function __construct($host, $user, $password, $db)
		{
			$this->con = mysql_connect($host, $user, $password);
			
			if ($this->con)
			{
				mysql_select_db($db, $this->con) or die("Could Not Select The Database !!");
			}
		}
		
		// Close the connection to database
		public function __destruct()
		{
			mysql_close($this->con);
		}
	
	
		#################################################
		#				General Functions				#
		#################################################

		public function query($sql)
		{
			$resource = mysql_query($sql);
	
			if ($resource)
			{
				if (is_resource($resource))
				{
					$i = 0;
					$data = array();
			
					while ($result = mysql_fetch_assoc($resource))
					{
						$data[$i] = $result;
						$i++;
					}
					
					mysql_free_result($resource);
					
					$query = new stdClass();
					$query->row = isset($data[0]) ? $data[0] : array();
					$query->rows = $data;
					$query->num_rows = $i;					
					unset($data);
					
					return $query;	
				}
				else
				{
					return true;
				}
			}
			else
			{
				exit('Error: ' . mysql_error() . '<br />Error No: ' . mysql_errno() . '<br />' . $sql);
			}
  		}		
	
		public function execute($command)
		{
			# Params:
			# 		$command = query command
			
			// For Operational query
			if 	(
				(stripos($command, "insert") !== false) ||
				(stripos($command, "update") !== false) ||
				(stripos($command, "delete") !== false) ||
				(stripos($command, "replace") !== false)
				)
			{
				mysql_query($command) or die(mysql_error());
				$this->affected = mysql_affected_rows();
				return $this->affected;
			}
			else
			{
				// For Selection query
				$this->result = mysql_query($command) or die(mysql_error());
				return $this->result;
			}
		}	
		
		public function count_all($table, $pk = "")
		{
			# Params:
			# 		$pk = Primary Key field name (If specified, the query will be much faster)
			
			if ($pk)
			{
				$this->result = mysql_query("select count($pk) as total from $table") or die(mysql_error());
				$this->row = mysql_fetch_array($this->result);
				return $this->row["total"];
			}
			else
			{
				$this->result = mysql_query("select count(*) as total from $table") or die(mysql_error());
				$this->row = mysql_fetch_array($this->result);
				return $this->row["total"];
			}
		}


		#################################################
		#				Utility Functions				#
		#################################################
		
		// Get the number of affected rows after Operational query has executed
		public function count_affected()
		{
			return $this->affected;
		}

		// Check whether a table has records		
		public function has_rows($table)
		{
			if ($this->count_all($table))
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/*
		public function escape($value) {
			return mysql_real_escape_string($value, $this->connection);
		}
		
		public function countAffected() {
			return mysql_affected_rows($this->connection);
		}
	
		public function getLastId() {
			return mysql_insert_id($this->connection);
		}	
		
		public function __destruct() {
			mysql_close($this->connection);
		}
		*/
		
	}
?>
Return current item: EZMVC