Location: PHPKode > scripts > Object based MySQL > object-based-mysql/mysql.php
<?php
/*
	@class: mysql
	@desc: Object based mysql class to insert/update/fetch data using objects
	@author: Tofeeq ur Rehman
*/
class mysql {

	private $db_link;
	private $result_set;
	private $EOF;
	public $total_rows;
	public $row;
	public $primary_key;
	public $debug = true;

	/*
		@function: constructor
		@args: database name, host, username, passowrd
	*/
	function mysql() {
		if ($arguments = func_get_args()) {
			$this->db_link = mysql_connect($arguments[1], $arguments[2], $arguments[3]);
			$this->triggerError(__LINE__, __FUNCTION__, __FILE__);
			mysql_select_db($arguments[0], $this->db_link);
			$this->triggerError(__LINE__, __FUNCTION__, __FILE__);
		} else if(defined("DB_NAME") && defined("DB_HOST") && defined("DB_USER") && defined("DB_PASS")) {
			$this->db_link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
			$this->triggerError(__LINE__, __FUNCTION__, __FILE__);
			mysql_select_db(DB_NAME, $this->db_link);
			$this->triggerError(__LINE__, __FUNCTION__, __FILE__);
		}
	}
	
	private function triggerError($line_no, $function, $file, $sql=null){
		if ($this->debug && mysql_error($this->db_link)) {
			echo 
			"<br><i><strong>Error:</strong></i> <span style='color:#ff0000;'><i>" . mysql_error($this->db_link) . "</i></span>" .
			"<br><i>Line: " . ($line_no-1) . ", Function: " . $function . "(...), File: " . $file . "</i>" .  
			"<br><i><strong>SQL:</strong> " . $sql . "</i>"
			;
		}
	}
	function getPrimaryKey($table, $debug=true){
		$sql = "SHOW FIELDS FROM `{$table}`";
		$this->query($sql);
		if ($debug) {
			$this->triggerError(__LINE__, __FUNCTION__, __FILE__);
		}
		while ($this->next()) {
			if ($this->row->Key == "PRI") {
				$this->primary_key = $this->row->Field;
				break;
			}
		}
		return $this->primary_key;
	}
	
	function query($sql, $debug=true) {
		$this->result_set = mysql_query($sql, $this->db_link);
		if ($debug) {
			$this->triggerError(__LINE__, __FUNCTION__, __FILE__, $sql);
		}
		if ($this->result_set) {
			!@$this->total_rows = $this->EOF = mysql_num_rows($this->result_set);
		}
	}

	function next() {
		$this->EOF--;
		if ($this->EOF > -1) {
			$this->row = mysql_fetch_object($this->result_set);
			return true;
		} else {
			$this->free();
			return false;
		}

	}

	function insert($table) {
		$str = "";
		foreach ($this->row as $col=>$val) {
			$str .= "`" . $col . "` = '" . $val . "', ";
		}
		$sql = "INSERT INTO `{$table}` SET " . rtrim($str, ", ");
		$this->query($sql, false);
		$this->triggerError(__LINE__, __FUNCTION__, __FILE__, $sql);
		$this->getPrimaryKey($table, false);
		$this->triggerError(__LINE__, __FUNCTION__, __FILE__, $sql);
		@$this->row->{$this->primary_key} = mysql_insert_id($this->db_link);
	}

	function update($table, $condition=false) {
		$str = "";
		foreach ($this->row as $col=>$val) {
			$str .= "`" . $col . "` = '" . $val . "', ";
		}
	
		if ($condition) {
			if (strpos($condition, "=") === false) {
				$this->getPrimaryKey($table, false);
				$this->triggerError(__LINE__, __FUNCTION__, __FILE__, $sql);
				$condition = "`{$this->primary_key}` = '" . $condition . "'";
			}
		}
		
		$sql = "UPDATE `{$table}` SET " . rtrim($str, ", ") . ($condition?" WHERE {$condition}":"");
		$this->query($sql, false);
		$this->triggerError(__LINE__, __FUNCTION__, __FILE__, $sql);
		return @mysql_affected_rows($this->db_link);
	}

	function free() {
		if ($this->result_set) {
			!@mysql_free_result($this->result_set);
		}
		$this->row = null;
	}
}
Return current item: Object based MySQL