Location: PHPKode > scripts > basic_db > basic_db/basic_db.php
<?
#Created by Ihab Abu Afia
#E-mail: hide@address.com

############################################# OPENNING CLASS DATABASE #############################################


/***********************************************************************
function db_connect()
********************************************************************** */

function db_connect()
{//begin function

	$host = "localhost";
	$username = "root";
	$password = "Ghadah11";
	
	$db_name = "bcard";

	$result = @mysql_connect($host,$username,$password);
	if(!$result)
		return false;

	if(!@mysql_select_db($db_name))
	return false;

	return $result;

}//end function

//***********************************************************************
// function db_result_to_array()
//***********************************************************************

function db_result_to_array($result)
{//begin function

	$res_array = array();

	for($count=0; $row = @mysql_fetch_array($result, MYSQL_ASSOC); $count++)
		{ $res_array[$count]= $row; }

	return $res_array; 
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//******************************************************************************
//***** CLASS DATABASE
//******************************************************************************
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class Database
{
    var $table;
    var $fields = "*";
    var $submit = "submit";
    var $doPrint;
    var $doPrint_r;
    var $where;
    var $vwhere;
    var $fwhere;
    var $kand;
    var $vand;
    var $fand;
    var $order;
    var $order_key;
    var $order_type;
	var $id;
	var $mfields = array();

	function Fields($fields = "*")
	{
        $this->fields = $fields;
	}
	
	function FieldsAdd($mfields)
	{
		foreach($mfields as $key => $value)
		{
        	$this->fields[$key] = $value;
		}
	}
	
	function Hidden($hidden = array())
	{
        $this->hidden = $hidden;
	}
	
	function Table_Name($table)
	{
        $this->table=$table;
	}

	function Submit($submit = "submit")
	{
        $this->submit=$submit;
	}
	
	function Where($where, $vwhere, $fwhere = "=")
	{
        $this->kwhere=$where;
        $this->fwhere=$fwhere;
        $this->vwhere=$vwhere;
	}

	function _And($kand, $vand, $fand = "=")
	{
        $this->kand=$kand;
        $this->fand=$fand;
        $this->vand=$vand;
	}

	function Query($query)
	{
        $this->query=$query;
	}
	
	function Date($field_name)
	{
        $this->fdate=$field_name;
	}

	function OrderBy ($order_key, $order_type = "ASC")
	{
		if($order_key && $order_type)
			{
				$this->order = true;
				$this->order_key = $order_key;
				$this->order_type = $order_type;
			}
	}
	
	function doPrint()
	{
		$this->doPrint = true;
	}
	
	function doPrint_r()
	{
		$this->doPrint_r = true;
	}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SELECT()
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	function Select($qtype='s')
	{
		$conn = db_connect();

		if($this->query)
		{
			$query = $this->query;
		}
		else
		{
			$query = "SELECT ";
		
			if(is_array($this->fields))
			{
				unset($this->fields[$this->submit]);
				foreach ($this->fields as $key => $row)
				{
					$query .= $row.", ";
				}
			
				$query = substr_replace($query,"",-2,-1);
			}
			else
			{
				$query .= "*";
			}
			
			$query .= " FROM $this->table";
			if($this->kwhere) $query .= " WHERE $this->kwhere $this->fwhere '$this->vwhere'";
			if($this->kand) $query .= " AND $this->kand $this->fand '$this->vand'";
			if($this->order) $query .= " ORDER BY $this->order_key $this->order_type";
		}
		$result = @mysql_query($query,$conn);
	
		if($this->doPrint)
			echo $query."<br />";
			
		if(!$result)
		{ return false; }
		else
		{
			if($qtype == 'm')
			{
				$result = db_result_to_array($result);
				if($this->doPrint_r)
					print_r($result);
				return $result;
			}
			else
			{
				$result = @mysql_fetch_array($result, MYSQL_ASSOC);
				if($this->doPrint_r)
					print_r($result);
				return $result;
			}
		}
	}	

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// INSERT()
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	
	function Insert()
	{
		$conn = db_connect();

		unset($this->fields[$this->submit]);
		
		if($this->hidden)
		{
			foreach ($this->hidden as $key => $row)
			{
				unset($this->fields[$row]);
			}
		}
		
		$query = "INSERT INTO ".$this->table." ( ";

		foreach ($this->fields as $key => $row)
		{
			$query .= $key.", ";
		}

		if($this->fdate)
		{
			$query .= $this->fdate.", ";
		}

		$query = substr_replace($query,"",-2,-1);

		$query .= " ) VALUES ( ";

		foreach ($this->fields as $key => $row)
		{
			$query .= " '".$row."', ";
		}

		if($this->fdate)
		{
			$query .= " NOW(), ";
		}
		$query = substr_replace($query,"",-2,-1);

		$query .= " ) ";
			
		if($this->doPrint)
			echo $query."<br />";

		$result = mysql_query($query, $conn);
		if(!$result)
		{ return false; }
		else
		{
			$id = mysql_insert_id();
			return $id;
		}
	}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// UPDATE()
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	function Update($id)
	{
		$value = $this->fields[$id];
		unset($this->fields[$this->submit]);
		unset($this->fields[$id]);
		
		if($this->hidden)
		{
			foreach ($this->hidden as $key => $row)
			{
				unset($this->fields[$row]);
			}
		}
		
		$conn = db_connect();
		$query = "UPDATE ".$this->table." SET ";

		foreach ($this->fields as $key => $row)
		{
			$query .= $key." = '".$row."', ";
		}

		if($this->fdate)
		{
			$query .= $this->fdate." = NOW(), ";
		}

		$query = substr_replace($query,"",-2,-1);

		$query .= " WHERE ".$id." = '".$value."'  ";
		
		if($this->doPrint)
			echo $query."<br />";
			
		$result = mysql_query($query, $conn);

		if(!$result)
		{ return false; }
		else
		{
			return true;
		}
	}	

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// DELETE()
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	function Delete($key, $value)
	{
		$conn = db_connect();
		$query = "DELETE FROM ".$this->table." WHERE $key = '".$value."'";
			
		if($this->doPrint)
			echo $query."<br />";
			
		$result = @mysql_query($query,$conn);
	
		if(!$result)
		{ return false; }
		else
		{
			return true;
		}
	}	
}

?>
Return current item: basic_db