MySQL Class

Class that interacts with MySQL, performing INSERT, UPDATE, DELETE and SELECT, at the moment

Usage

$single = "";
$multi = array();

$mySQL = new MySQL(
			array(
				"Server" => "localhost",
				"Username" => "Username",
				"Password" => "Password",
				"Database" => "Database"
			)
		);

/* Without Errors or use above
$mySQL = new MySQL(
			array(
				"Server" => "localhost",
				"Username" => "Username",
				"Password" => "Password",
				"Database" => "Database"
			),
			false
		);
*/

/* With Errors
$mySQL = new MySQL(
			array(
				"Server" => "localhost",
				"Username" => "Username",
				"Password" => "Password",
				"Database" => "Database"
			),
			true
		);
*/

$mySQL->open();
$mySQL->selectWhere("TableName", null, array("id" => 1));
while($row = $mySQL->results()) {
	$single = new ValueObject($row);
	$single->toString();
}
$mySQL->close();

$mySQL->open();
$mySQL->select("TableName");
while($row = $mySQL->results())
	$multi[] = new ValueObject($row);

$mySQL->close();

for($a = 0; $a < sizeof($multi); $a++)
	$multi[$a]->toString();

$mySQL->open();
$mySQL->insert("TableName", array("value1" => 789, "value2" => "TEST6", "value3" => "2010-06-19", "value4" => 6));
$mySQL->close();

$mySQL->open();
$mySQL->update("TableName", array("value1" => 789, "value2" => "TEST3", "value3" => "2010-06-18", "value4" => 3), array("id" => 3));
$mySQL->close();

$mySQL->open();
$mySQL->delete("TableName", array("id" => 3));
$mySQL->close()

Code

class MySQL {

	private $properties = array(
					"ConnectionProperties" => array(
										"Server" => null,
										"Username" => null,
										"Password" => null,
										"Database" => null
									),
					"Connection" => null,
					"Result" => null,
					"InsertID" => null,
					"Error" => array(
									"SetError" => false
								)
					);

	public function __construct($values = array(), $error = false) {
		foreach($values as $key => $value)
			$this->properties["ConnectionProperties"][$key] = $value;

		$this->properties["Error"]["SetError"] = $error;
	}

	public function __set($key = "", $value = "") {
		$this->properties[$key] = $value;
	}

	public function __get($key = "") {
		if(array_key_exists($key, $this->properties))
			return $this->properties[$key];
	}

	public function open() {
		$this->properties["Connection"] = mysql_connect($this->properties["ConnectionProperties"]["Server"], $this->properties["ConnectionProperties"]["Username"], $this->properties["ConnectionProperties"]["Password"]);
		if(!$this->properties["Connection"]) {
			$this->error();
		} else
			mysql_select_db($this->properties["ConnectionProperties"]["Database"], $this->properties["Connection"]);
	}

	public function close() {
		if(mysql_insert_id($this->properties["Connection"]) != 0 || mysql_insert_id($this->properties["Connection"]) != null || mysql_insert_id($this->properties["Connection"]) != false)
			if($this->properties["Result"]) {
				mysql_free_result($this->properties["Result"]);
				$this->properties["Result"] = null;
			}

		mysql_close($this->properties["Connection"]);
	}

	public function select($table = null, $columns = null, $condition = array()) {
		$conditionSize = sizeof($condition);
		$columnSize = sizeof($columns);
		$a = 0;
		$column = "";
		$where = "";

		if($columns == null) {
			$column .= "*";
		} else {
			for($a = 0; $a < $columnSize; $a++) {
				if($a < ($columnSize - 1))
					$column .= $columns[$a].", ";
				else
					$column .= $columns[$a];
			}
		}

		if($condition == null)
			$where .= " ";
		else {
			$where .= " WHERE ";
			foreach ($condition as $key => $value) {
				if(is_numeric($value)) {
					if($a == ($conditionSize - 1))
						$where .= $key.' = '.$value;
					else
						$where .= $key.' = '.$value.' AND ';
				} else {
					if($a == ($conditionSize - 1))
						$where .= $key." = '".$value."'";
					else
						$where .= $key." = '".$value."' AND ";
				}
				$a++;
			}
		}

		if(!$this->properties["Result"] = mysql_query("SELECT ".$column." FROM ".$table.$where, $this->properties["Connection"])) {
			$this->error();
		}
	}

	public function insert($table = "", $values = array()) {
		$size = sizeof($values);
		$columns = "";
		$temp = "";

		$a = 0;
		foreach ($values as $key => $value) {
			if($a == ($size - 1))
				$columns .= $key;
			else
				$columns .= $key.', ';

			$a++;
		}

		$a = 0;
		foreach ($values as $key => $value) {
			if(is_numeric($value)) {
				if($a == ($size - 1))
					$temp .= $value;
				else
					$temp .= $value.', ';
			} else {
				if($a == ($size - 1))
					$temp .= "'".$value."'";
				else
					$temp .= "'".$value."', ";
			}
			$a++;
		}

		if(!$this->properties["Result"] = mysql_query("INSERT INTO ".$table." (".$columns.") VALUES (".$temp.")", $this->properties["Connection"])) {
			$this->error();
		} else
			$this->properties["InsertID"] = mysql_insert_id($this->properties["Connection"]);
	}

	public function update($table = "", $values = array(), $condition = array()) {
		$size = sizeof($values);
		$conditionSize = sizeof($condition);
		$temp = "";

		$a = 0;
		foreach ($values as $key => $value) {
			if(is_numeric($value)) {
				if($a == ($size - 1))
					$temp .= $key." = ".$value;
				else
					$temp .= $key." = ".$value.', ';
			} else {
				if($a == ($size - 1))
					$temp .= $key." = "."'".$value."'";
				else
					$temp .= $key." = "."'".$value."', ";
			}
			$a++;
		}

		$where = " WHERE ";
		$a = 0;
		foreach ($condition as $key => $value) {
			if(is_numeric($value)) {
				if($a == ($conditionSize - 1))
					$where .= $key.' = '.$value;
				else
					$where .= $key.' = '.$value.' AND ';
			} else {
				if($a == ($conditionSize - 1))
					$where .= $key." = '".$value."'";
				else
					$where .= $key." = '".$value."' AND ";
			}
			$a++;
		}
		if(!$this->properties["Result"] = mysql_query("UPDATE ".$table." SET ".$temp.$where, $this->properties["Connection"])) {
			$this->error();
		}
	}

	public function delete($table = "", $condition = array()) {
		$conditionSize = sizeof($condition);
		$where = " WHERE ";
		$a = 0;

		foreach ($condition as $key => $value) {
			if(is_numeric($value)) {
				if($a == ($conditionSize - 1))
					$where .= $key.' = '.$value;
				else
					$where .= $key.' = '.$value.' AND ';
			} else {
				if($a == ($conditionSize - 1))
					$where .= $key." = '".$value."'";
				else
					$where .= $key." = '".$value."' AND ";
			}
			$a++;
		}

		if(!$this->properties["Result"] = mysql_query("DELETE FROM ".$table.$where, $this->properties["Connection"])) {
			$this->error();
		}
	}

	public function results() {
		return mysql_fetch_object($this->properties["Result"]);
	}

	private function error() {
		if($this->properties["Error"]["SetError"])
			echo "MySQL Error: (". mysql_errno().") ".mysqlerror()."<br />";
	}
}
tagged in:

comment  ↓