Location: PHPKode > projects > Aratix > aratix/classes/Mysql.class.php
<?php
/*
** Aratix
** Version 0.1.0beta8 (Anubis)
**
** Autor: Manuel Burghammer
** E-Mail: hide@address.com
** Homepage: http://www.xitara.net
**
** File: Mysql.class.php - Version 0.1.0 build 122
** Position: /classes/
** Last Modified: 09.06.2006 - 21:18:09
*/

class Mysql{
	function connect($db_host, $db_user, $db_pass){
		$this->connect_id = @mysql_connect($db_host, $db_user, $db_pass);

		if(!$this->connect_id){
			echo mysql_error();
			return FALSE;
		}
		else
			return TRUE;
	}
	function disconnect($db_connect_id = ''){
		if($db_connect_id == "")
			$db_connect_id = $this->connect_id;

		$this->close_id = @mysql_close($db_connect_id);

		if(!$this->close_id){
			echo mysql_error();
			return FALSE;
		}
		else
			return TRUE;
	}
	function select_db($db){
		$this->select_id = @mysql_select_db($db);

		if(!$this->select_id){
			echo mysql_error();
			return FALSE;
		}
		else
			return TRUE;
	}
	function query($db_query_string){
		$GLOBALS['debug']->write(DEBUG, "Query: " . $db_query_string, __FILE__, __CLASS__, __METHOD__, __FUNCTION__, __LINE__);

		if(!($query = mysql_query($db_query_string)))
			return false;
		else{
			if(strpos(strtolower($db_query_string), "select") === FALSE){
				$rows = @mysql_affected_rows();
				return array('rows' => $rows, 'data' => "");
			}
			else{
				$rows = @mysql_num_rows($query);
				$data = @mysql_fetch_array($query);
				return array('rows' => $rows, 'data' => $data);
			}
		}
	}
	function read($db_table, $db_fields, $db_where=""){
		$db_query_string = "SELECT " . $db_fields . " FROM " . $db_table;

		if($db_where != "")
			$db_query_string .= " WHERE " . $db_where;

// 		$db_result = mysql_query(mysql_real_escape_string($db_query_string));
		$db_result = mysql_query($db_query_string);

		$GLOBALS['debug']->write(DEBUG, "Query: " . $db_query_string, __FILE__, __CLASS__, __METHOD__, __FUNCTION__, __LINE__);

		if(!$db_result)
			return FALSE;

		$this->row = mysql_fetch_array($db_result);

		if($this->row)
			mysql_free_result($db_result);

		return $this->row;
	}
	function read_array($db_table, $db_fields, $db_where="", $db_order="", $db_limit=""){
		$db_query_string = "SELECT " . $db_fields . " FROM " . $db_table;

		if($db_where != "")
			$db_query_string .= " WHERE " . $db_where;
		if($db_order != "")
			$db_query_string .= " ORDER BY " . $db_order;
		if($db_limit != "")
			$db_query_string .= " LIMIT " . $db_limit;

		$GLOBALS['debug']->write(DEBUG, "Query: " . $db_query_string, __FILE__, __CLASS__, __METHOD__, __FUNCTION__, __LINE__);

		$result = @mysql_query($db_query_string);

		if(!$result)
			die('Lesen fehlgeschlagen: ' . mysql_error() . " -> " . $db_query_string);

		$db_rows = @mysql_num_rows($result);
		$db_array = array();

		for($db_count = 0; $db_count < $db_rows; $db_count++){
			$db_fetch = @mysql_fetch_array($result);
			array_push($db_array, $db_fetch);
		}
		@mysql_free_result($result);

//		if($db_rows == 0)
			return $db_array;

//		return FALSE;
	}
	function write($db_table, $db_set, $db_data){
		if(count($db_set) != count($db_data))
			die('Feldanzahl stimmt nicht mit Wertanzahl berein');

		$db_query_string = "INSERT INTO " . $db_table . " SET ";
		$db_set_array = explode(";;", $db_set);
		$db_data_array = explode(";;", $db_data);

//		if(count($db_set_array) == 1)
//			$db_set_array = explode(",", $db_set);
//		if(count($db_data_array) == 1)
//			$db_data_array = explode(",", $db_data);

		for($db_count = 0; $db_count < count($db_set_array); $db_count++){
			if(db_check_quotes_needed($db_data_array[$db_count]))
				$db_single_quote = "'";
			else
				$db_single_quote = "";

			$db_query_string .= mysql_real_escape_string($db_set_array[$db_count]) . "=" . $db_single_quote . mysql_real_escape_string(htmlentities($db_data_array[$db_count])) . $db_single_quote;

			if($db_count < count($db_set_array) - 1)
				$db_query_string .= ", ";

		}
		$db_result = mysql_query($db_query_string);

		$GLOBALS['debug']->write(DEBUG, "Query: " . $db_query_string, __FILE__, __CLASS__, __METHOD__, __FUNCTION__, __LINE__);

		if(!$db_result)
			return -1;

		$last_id = @mysql_insert_id();
		$GLOBALS['debug']->write(DEBUG, "Insert ID: " . $last_id, __FILE__, __CLASS__, __METHOD__, __FUNCTION__, __LINE__);

		return $last_id;
	}
	function update($db_table, $db_set, $db_data, $db_where = ""){
		if(count($db_set) != count($db_data))
			die('Feldanzahl stimmt nicht mit Wertanzahl berein');

		$db_query_string = "UPDATE " . $db_table . " SET ";
		$db_set_array = explode(";;", $db_set);
		$db_data_array = explode(";;", $db_data);

//		if(count($db_set_array) == 1)
//			$db_set_array = explodeTB_PREFIX . get('art') . "s", (",", $db_set);
//		if(count($db_data_array) == 1)
//			$db_data_array = explode(",", $db_data);

		for($db_count = 0; $db_count < count($db_set_array); $db_count++){
			if(db_check_quotes_needed($db_data_array[$db_count]))
				$db_single_quote = "'";
			else
				$db_single_quote = "";

//			$db_data = str_replace(":::", ";", $db_data_array[$db_count]);
//			$db_data = str_replace("###", ",", $db_data);

			$db_query_string .= mysql_real_escape_string($db_set_array[$db_count]) . "=" . $db_single_quote . mysql_real_escape_string(htmlentities($db_data_array[$db_count])) . $db_single_quote;

			if($db_count < count($db_set_array) - 1)
				$db_query_string .= ", ";

		}
		if($db_where != "")
			$db_query_string .= " WHERE " . $db_where;

		$GLOBALS['debug']->write(DEBUG, "Query: " . $db_query_string, __FILE__, __CLASS__, __METHOD__, __FUNCTION__, __LINE__);
		$db_result = mysql_query($db_query_string);
		$db_num = mysql_affected_rows();

		if($db_num == 0)
			return FALSE;
		else
			return $db_num;
	}
	function delete($db_table, $db_where){
		$db_query_string = "DELETE FROM " . $db_table;

		if($db_where != "")
			$db_query_string .= " WHERE " . $db_where;

		$db_result = mysql_query($db_query_string);

		$GLOBALS['debug']->write(DEBUG, "Query: " . $db_query_string, __FILE__, __CLASS__, __METHOD__, __FUNCTION__, __LINE__);

		if(!$db_result)
			return FALSE;
		else
			return TRUE;
	}
	function check_table($db_table){
		$db_query = @mysql_query("SELECT 1 FROM $db_table LIMIT 0");

		if($db_query)
			return TRUE;
		else
			return FALSE;
	}
}

function db_check_quotes_needed($db_data){
	$db_keywords = array(	"ASCII",
							"CHAR",
							"MD5",
							"SHA1",
							"ENCRYPT",
							"RAND",
							"LAST_INSERT_ID",
							"COUNT",
							"AVG",
							"SUM",
							"SOUNDEX",
							"LCASE",
							"UCASE",
							"NOW",
							"PASSWORD",
							"CURDATE",
							"CURTIME",
							"CURRENT_TIMESTAMP",
							"FROM_DAYS",
							"FROM_UNIXTIME",
							"PERIOD_ADD",
							"PERIOD_DIFF",
							"TO_DAYS",
							"UNIX_TIMESTAMP",
							"USER",
							"WEEKDAY",
							"CONCAT");

	foreach($db_keywords as $db_keyword){
		$db_pos = strpos($db_data, $db_keyword);

		if($db_pos !== FALSE)
			return FALSE;
	}
	return TRUE;
}
?>
Return current item: Aratix