Location: PHPKode > scripts > c_mysql > c_mysql/c_mysql.php
<?php
	class c_mysql
	{
		/*--------------------------------------------------+
		|	c_mysql class									|
		+---------------------------------------------------+
		|	handels connection to MySql datadase			|
		|	and executes queryes on it						|
		+---------------------------------------------------+
		|	author: fizk (hide@address.com)				|
		|	created: 1. feb. 2004							|
		|	last modyfied : 8. sep 2004						|
		+---------------------------------------------------+
		|	documentation:									|
		|		read "readme.htm"							|
		+--------------------------------------------------*/
		
		
	
//-- CLASS CONFIGURATIN VARIABLES---------------------------------------------------------	
		//connection variables
		var $host = "localhost";							//name of host
		var $user = "root";									//name of user
		var $pass = "";										//password for mysql
		var $db = "";										//name of database
		var $table = false;									//name of table
		
		//debug variables		
		var $debugg=1;										//true for error report, false for echo "c_mysql fatal error"
		var $error_log_ = 1;								//true for writing an error log txt file, false for not
		var $error_log_path = "c_mysql_error_log.txt";		//path to c_mysql_error_log.txt
		
		//limit variables
		var $per_page = 5;									//how many enteries are displayed on a page
		
		//format variables
		var $format = "array";								//how to retrun a query -> can be array or xml
		
		//xml variables
		var $uni_xsl = false;								//"MessageLog.xsl";
		var $uni_format = "iso-8859-1";						//default xml format
		var $uni_version = "1.0";							//xml version number

//-- END OF CLASS CONFIGURATIN VARIABLES---------------------------------------------------------


//don't modify beond this point

		//class variables
		var $connection = false;								// holds the connection handler
		
		var $lim_total = false;									//total number of enteries withput the LIMIT
		var $lim_pre = false;									//is there a reason for printing a pre link
		var $lim_next = false;									//is there a reason for printing a next link
		var $lim_to = 0;										//index number for pre link
		var $lim_from = 0;										//index number for next link

/*--------------------------------------+
|	private methods:					|
+---------------------------------------+
|	c_mysql_close()						|
|		closes að connection to MySql	|
|	arr_return							|
|		makes the return array from a	|
|		query							|
|	xml_return							|
|		makes the xml return string		|
|		from a query					|
+--------------------------------------*/
	
//--c_mysql_connect()------------------------------------------------------------------------		
		function c_mysql_connect()	//connect to database
									//and select a database
		{
			$this->connection = @mysql_connect($this->host,$this->user,$this->pass);
										if($this->connection==false)
										{
											$this->error_01("c_mysql_connect()","can´t connect to MySql");
											return false;
										}
			$sel_db = @mysql_select_db($this->db);
										if($sel_db==false)
										{
											$this->error_01("c_mysql_connect()","can't select database : ".$this->db);
											return false;
										}
		}
//--------c_mysql_close--------------------------------------------------------------------------------		
		function c_mysql_close()	//close the database connection
		{
			$con_close = @mysql_close($this->connection);
										if($con_close==false)
										{
											$this->error_01("c_mysql_close()","can't close database");
											return false;
										}
		}
//---------arr_return------------------------------------------------------------------------------------
		function arr_return($result,$met)	//returns a query in an array
		{			
					$return_array = array();
				// how many rows in this table
					$num=@mysql_num_rows($result);
								if($num==false)
								{
									return false;
								}
				//stores total number of rows
					$this->c_mysql_num = $num;
				// number of affected fields			
					$num_fiel = @mysql_num_fields($result);
								if($num_fiel==false)
								{
									return false;
								}
				// make return array 
					for($i=0;$i<$num;$i++)															//loop through affected rows
					{
							for($a=0;$a<$num_fiel;$a++)												// and with in it loop through each field in a row
							{
								$curr_key = mysql_field_name($result,$a);							//get the name of the field
								$return_array[$i][$curr_key] = mysql_result($result,$i,$curr_key); // put the value in the array					
							}
					}
					return $return_array;			
		}
		
//-------xml_return--------------------------------------------------------------------------
		function xml_return($result)
		{
			//start return string
			$return_string = "<?xml version=\"" . $this->uni_version . "\" encoding=\"" . $this->uni_format . "\"?>\n";
			$return_string .= ($this->uni_xsl)?"<?xml-stylesheet type='text/xsl' href='" . $this->uni_xsl . "'?>\n":"";
			$num_fields = mysql_num_fields($result);
			$num_rows = mysql_num_rows($result);
			// print root tag
			$return_string .= "<" . $this->db . " fields=\"" . $num_fields . "\" rows=\"" . $num_rows . "\">\n";
			
					// how many rows in this table
					$num=@mysql_num_rows($result);
								if($num==false)
								{
									return false;
								}
				//stores total number of rows
					$this->c_mysql_num = $num;
				// number of affected fields			
					$num_fiel = @mysql_num_fields($result);
								if($num_fiel==false)
								{
									return false;
								}

				// make return array 
					
				for($i=0;$i<$num;$i++)															//loop through affected rows
					{
					$return_string .= "\t<" . $this->table . ">\n";
							for($a=0;$a<$num_fiel;$a++)												// and with in it loop through each field in a row
							{
								$curr_key = mysql_field_name($result,$a);							//get the name of the field
								$curr_value = htmlspecialchars(mysql_result($result,$i,$curr_key),ENT_QUOTES); // put the value in the array					
								$curr_type = mysql_field_type($result,$a);
								$curr_length = mysql_field_len($result,$a);
								$curr_flag = mysql_field_flags($result,$a);

								$return_string .= "\t\t<" . $curr_key . " type=\"" . $curr_type . "\" length=\"" . $curr_length . "\" flags=\"" . $curr_flag . "\">";
								$return_string .= $curr_value;
								$return_string .= "</" . $curr_key . ">\n";
								
							}
					$return_string .= "\t</" . $this->table . ">\n";
					}
		
			
			// print end root tag
			$return_string .= "</" . $this->db . ">\n";
			
			//return well formated xml string
			return $return_string;
		}
						
/*--------------------------------------+
|	public methods:						|
+---------------------------------------+
|	c_mysql_connect()					|
|		connects to MySql				|
|	c_mysql_SELECT()					|
|		select query					|
|	c_mysql_UPDATE()					|
|		update query					|
|	c_mysql_INSERT()					|
|		insert query					|
|	c_mysql_DELETE()					|
|		delete query					|
|	c_mysql_LIMIT()						|
|		limt query						|
|	c_mysql_per_page()					|
|		entery per page					|
|	c_mysql_set_format()				|
|		xml or array					|
+--------------------------------------*/

//--c_mysql()--------------------------------------------------------------------------------		
		function c_mysql()	//Constructor function
							//poppulates connection varaiables if passed
		{
			$num_args = func_num_args();
			if($num_args != 0)		//if argument are passed to the function
			{
				if($num_args == 4)	// if arguments passed are 4 then poppulate connection variables with new values
				{
					$this->host = func_get_arg(0);
					$this->user = func_get_arg(1);
					$this->pass = func_get_arg(2);
					$this->db = func_get_arg(3);
				}
				else				// else the class will exit and a error report issued
				{
					$this->error_01("c_mysql(constructer)","invalit number of arguments were passed to the Constructor function (arguments passed " . $num_args . ", requires 4)","c_mysql()");
					return false;
				}
			}
		}
		
//--SELECT----------------------------------------------------------------------------------
		function c_mysql_SELECT($table, $fields, $where="", $order="", $desc=false)
		{
			$this->table = $table;
			// make the query_string
				$query_string = "SELECT $fields FROM $table ";
				if($where != "")
				{
					$query_string .= "WHERE $where ";
				}
				if($order != "")
				{
					$query_string .= "ORDER BY $order ";
				}
				if($desc)
				{
					$query_string .= "DESC ";
				}
				$query_string .=";";
			//open connection
				$this->c_mysql_connect();
			// send the query
				$result=@mysql_query($query_string);
							if($result==false)
							{
								$this->error_02("c_mysql_SELECT()",$query_string);
								return false;
							}
			//close connection
				$this->c_mysql_close();
			//return output
				if($this->format=="array")
				{
					return $this->arr_return($result,"c_mysql_SELECT()");				
				}
				else
				{
					return $this->xml_return($result,"c_mysql_SELECT()");
				}
			//disconnect	
				$this->c_mysql_close();
		}
//--UPDATE-----------------------------------------------------------------------------------------
		function c_mysql_UPDATE($table=false,$val_arr=false,$condition=false)					// execute an update query
		{
			$this->table = $table;
			// check if right argument are passed
			$num_args = func_num_args();
			if($num_args!=3)	//are argument 3
			{
				$this->error_03("c_mysql_UPDATE()","invalid number of arguments passed");
				return false;
			}
			if(is_string(func_get_arg(0))!=true)//is argument 1 a string
			{
				$this->error_03("c_mysql_UPDATE()","function argument 1 is not a string");
				return false;
			}
			if(is_array(func_get_arg(1))!=true)//is argument 2 an array
			{
				$this->error_03("c_mysql_UPDATE()","function argument 2 is not an array");
				return false;
			}
			if(is_string(func_get_arg(2))!=true)//is argument 3 a string
			{
				$this->error_03("c_mysql_UPDATE()","function argument 3 is not a string");
				return false;
			}
			//connect
				$this->c_mysql_connect();

				$field_array=false;		//will hold all fields in a given table in an array. field-name = key, false = value
				$new_val_arr=false;		//this array will hold only keys that coresponds with names a field in a given table and the values that are to be updated

				$fields = @mysql_list_fields($this->db, $table);
								if($fields==false)
								{
									$this->error_03("c_mysql_UPDATE()","can´t list fields");
									return false;
								}
				$columns = @mysql_num_fields($fields);
								if($columns==false)
								{
									$this->error_03("c_mysql_UPDATE()","can´t get number of fields");
									return false;
								}
				for ($i = 0; $i < $columns; $i++) 
				{
				   $field_array[mysql_field_name($fields, $i)]=false;
				}
				foreach($val_arr as $key => $value)
				{
					if(array_key_exists( $key, $field_array))
					{
						$new_val_arr[$key] = $value;
					}
				}
			//holds query string
				$query_string=false;
			//count the val_arr
				$val_arra_count = count($new_val_arr);
			//make query string
				$query_string = "UPDATE $table SET ";
				$c=0;
				
				if($new_val_arr==false)
				{
					$this->error_03("c_mysql_UPDATE()","no values to update");
					return false;
				}
				foreach($new_val_arr as $key => $value)
				{
					$query_string .= "$key='$value'";
					
					if($c!=$val_arra_count-1)
					{
						$query_string .= ", ";
					}
					$c++;
				}
				$query_string .= " WHERE $condition;";
			//send the query
				$return = @mysql_query($query_string);
								if($return==false)
								{
									$this->error_02("c_mysql_UPDATE()",$query_string);
									return false;
								}
				$this->c_mysql_close();
			return true;
		}
//--INSERT--------------------------------------------------------------------------------------
		function c_mysql_INSERT($table,$val_arr)	// execute an insert query
		{
			$this->table = $table;
			if(is_array($val_arr)==false)	// was there an array passed?
			{
				$this->error_01("c_mysql_INSERT()","secound argument is not an array");
				return false;			
			}
			if(is_string($table)==false)
			{
				$this->error_01("c_mysql_INSERT()","first argument is not a string");
				return false;						
			}
			$num_args = func_num_args();
			if($num_args!=2)
			{
				$this->error_01("c_mysql_INSERT()","invalid number of argument passed, (".$num_args." of 2)");
				return false;						
			}
			$field_name_array;
			$this->c_mysql_connect();
				$select_result = @mysql_query("SELECT * FROM $table");
									if($select_result==false)
									{
										$this->error_02("c_mysql_INSERT()","SELECT * FROM $table");
										return false;
									}
			//number of fiels in this row
				$field_number = @mysql_num_fields($select_result);
									if($field_number==false)
									{
										$this->error_02("c_mysql_INSERT()","SELECT * FROM $table");
										return false;
									}
			// make return array
				for($i=0;$i<$field_number;$i++)								
				{
					$curr_key = mysql_field_name($select_result,$i);
					$field_name_array[$curr_key] = "";
				}
			//count return array
				$field_name_array_count = count($field_name_array);			
			//populete return array
				foreach($val_arr as $key => $value)							
				{
					//if database array and incoming array have the same key populate the value
					if(array_key_exists($key,$field_name_array))			
					{
						$field_name_array[$key] = $value;
					}
				}
			// make a query string
				$sql_query_sting = "INSERT INTO $table(";					
				$c = 0;
				foreach($field_name_array as $key => $value)
				{
					$sql_query_sting .= $key;
					if($c!=$field_name_array_count-1)
					{
						$sql_query_sting .= ", ";
					}
					
					$c++;
				}
				$sql_query_sting .= ") VALUES (";
				$c = 0;
				foreach($field_name_array as $key => $value)
				{
					$sql_query_sting .= "'$value'";
					if($c!=$field_name_array_count-1)
					{
						$sql_query_sting .= ", ";
					}
					$c++;
				}
				$sql_query_sting .= ");";
			// send the query
				$result = @mysql_query($sql_query_sting);
							if($result==false)
							{
								$this->error_02("c_mysql_INSERT()",$sql_query_sting);
								return false;
							}						
			//disconnect
			$this->c_mysql_close();
			// return
			return true;		
		}
//--DELETE------------------------------------------------------------------------------------------
		function c_mysql_DELETE($table,$condition)	// execute a delete query
		{
			$num_args = func_num_args();
			if($num_args!=2)
			{
				$this->error_01("c_mysql_DELETE()","invalid number of argument passed, (".$num_args." of 2)");
				return false;						
			}
			//open connection
				$this->c_mysql_connect();
			//store qurty_string in a variable
				$query="DELETE FROM $table WHERE $condition";
			// send the query
				$result=mysql_query($query);
								if($result==false)
								{
									$this->error_02("c_mysql_DELETE()",$query);
									return false;
								}
			//close connection
				$this->c_mysql_close();
			//return			
				return true;
		}
//--LIMIT---------------------------------------------------------------------------------------------
		function c_mysql_LIMIT($table, $field, $where, $order, $ix=0, $desc=false)
		{
			$this->c_mysql_connect();	//connect
			//get total number of rows (with out LIMIT)
			$SQL="SELECT COUNT(*) AS Total FROM " . $table . " WHERE " . $where ;
			$SQL_Result=mysql_db_query($this->db, $SQL);
			$SQL_Result_Array=mysql_fetch_array($SQL_Result);
			$this->lim_total=$SQL_Result_Array['Total'];
			
			$query_string = "SELECT $field FROM $table WHERE $where ORDER BY '$order' ";
			if($desc)
			{
				$query_string .= "DESC ";
			}
			$query_string .= "LIMIT $ix, " . $this->per_page . ";";
			
				$result=@mysql_query($query_string);
							if($result==false)
							{
								$this->error_02("c_mysql_LIMIT()",$query_string);
								return false;
							}
			//close connection
				$this->c_mysql_close();
			//populate c_mysql properties
				$this->lim_to =  $ix + $this->per_page;		//next query
				$this->lim_from =  $ix - $this->per_page;	//prev query
	
				if($this->lim_to >= $this->lim_total)
				{
					$this->lim_next = false;
				}
				else
				{
					$this->lim_next = true;
				}
				if($this->lim_from < 0)
				{
					$this->lim_pre = false;
				}
				else
				{
					$this->lim_pre = true;
				}
				if($this->format=="array")
				{
					return $this->arr_return($result,"c_mysql_LIMIT()");
				}
				else
				{
					return $this->xml_return($result);
				}
				
			//disconnect					
				$this->c_mysql_close();		
		}
//-- COUNT--------------------------------------------------------------------------------------------
	function c_mysql_COUNT($table,$where)
	{
		//connect
			$this->c_mysql_connect();
		//count action
			$SQL="SELECT COUNT(*) AS Total FROM " . $table . " WHERE " . $where ;
			$SQL_Result=@mysql_db_query($this->db, $SQL);
					if(SQL_Result==false)
					{
						$this->error_02("c_mysql_count()",$SQL);
						return 0;
					}
			$SQL_Result_Array=@mysql_fetch_array($SQL_Result);
					if($SQL_Result_Array==false)
					{
						$this->error_02("c_mysql_count()",$SQL);
						return 0;					
					}
			$this->lim_total=$SQL_Result_Array['Total'];
		//disconnect
			$this->c_mysql_connect();
		//return
			return $this->lim_total;	
		
	}
//--c_mysql_per_page--------------------------------------------------------------------------------
		function c_mysql_per_page($a)
		{
			$this->per_page = $a;
		}
//----c_mysql_set_format-------------------------------------------------------------
		function c_mysql_set_format($str)
		{
			$this->format = $str;
		}
//----c_mysql_set_xsl
		function c_mysql_set_xsl($str)
		{
			$this->uni_xsl = $str;
		}
/*--------------------------------------+
|	error methods:						|
+---------------------------------------+
|		error_01()						|
|		error_02()						|
|		error_03()						|
+--------------------------------------*/
	function error_01($met,$msg)
	{

			if($this->debugg==1)				//write an error log to the browser
			{
				echo "<b>c_mysql fatal error</b><br><br>\n";
				echo $msg . "<br><br>\n",
				 	"<b>host : </b>" . $this->host . "<br>\n",
					"<b>user </b>: " . $this->user . "<br>\n",
					"<b>password </b>: " . $this->pass . "<br>\n",
					"<b>database </b>: " . $this->db . "<br>\n",
					"<b>method </b>: " . $met. "\n";
			}
			if($this->error_log_==1)			//write an error log to the disc
			{
				$for_date = date("D. M. d. Y.  G:i:s");
				$handle = fopen($this->error_log_path, "a");
				fwrite($handle,"\n$for_date\n\t fatal error in class c_mysql\n\t ". $msg ."\n\t method : " .$met . "\n");
				fclose($handle);
			}
	}
	function error_02($met,$sql="none")
	{
			if($this->debugg==1)				//write an error log to the browser
			{
				echo "<b>c_mysql error</b><br><br>\n";
				echo mysql_error() . "<br><br>\n",
				 	"<b>host : </b>" . $this->host . "<br>\n",
					"<b>user </b>: " . $this->user . "<br>\n",
					"<b>password </b>: " . $this->pass . "<br>\n",
					"<b>database </b>: " . $this->db . "<br>\n",
					"<b>table </b>: " . $this->table . "<br>\n",
					"<b>SQL </b>: " . $sql . "<br>\n",
					"<b>method </b>: " . $met. "\n";
									
			}
			if($this->error_log_==1)			//write an error log to the disc
			{
				$for_date = date("D. M. d. Y.  G:i:s");
				$handle = fopen($this->error_log_path, "a");
				fwrite($handle,"\n$for_date\n\t error in class c_mysql\n\t ". mysql_error() ."\n\t method : " .$met . "\n");
				fclose($handle);
			}
	}
	function error_03($met,$msg,$sql="none")
	{
			if($this->debugg==1)				//write an error log to the browser
			{
				echo "<b>c_mysql error</b><br><br>\n";
				echo $msg . "<br><br>\n",
				 	"<b>host : </b>" . $this->host . "<br>\n",
					"<b>user </b>: " . $this->user . "<br>\n",
					"<b>password </b>: " . $this->pass . "<br>\n",
					"<b>database </b>: " . $this->db . "<br>\n",
					"<b>table </b>: " . $this->table . "<br>\n",
					"<b>SQL </b>: " . $sql . "<br>\n",
					"<b>method </b>: " . $met. "\n";
			}
			if($this->error_log_==1)			//write an error log to the disc
			{
				$for_date = date("D. M. d. Y.  G:i:s");
				$handle = fopen($this->error_log_path, "a");
				fwrite($handle,"\n$for_date\n\t error in class c_mysql\n\t ". $msg ."\n\t method : " .$met . "\n");
				fclose($handle);
			}
	}
//end class
}
?>
Return current item: c_mysql