<?
    set_time_limit(0);
	class mysql {
		private $linkid;		// MySQL link identifier
		private $host;			// MySQL server host
		private $user;			// MySQL user
		private $pswd;			// MySQL password
		private $db;			// MySQL database
		private $result;		// Query result
		private $querycount;	// Total queries executed
		
		// Class constructor. Initializes the $host, $user, $pswd and $db fields.
		function __construct($host, $user, $pswd, $db) {
			$this->host = $host;
			$this->user = $user;
			$this->pswd = $pswd;
			$this->db = $db;
		}
		
		// Connects to the MySQL database server.
		function connect() {
			try {
				$this->linkid = @mysql_connect($this->host, $this->user, $this->pswd);
				if (! $this->linkid) 
					throw new Exception("Could not connect to the MySQL server.");
			}
			catch (Exception $e) {
				die($e->getMessage());
			} 
		}
		
		// Selects the MySQL database
		function select() {
			try {
				if (! @mysql_select_db($this->db, $this->linkid))
					throw new Exception("Could not connect to the MySQL database.");
			}
			catch (Exception $e) {
				die($e->getMessage());
			}
		}
		
		// Closes the MySQL database
		function close() {
			try {
				if (! @mysql_close($this->linkid))
					throw new Exception("Could not close the MySQL database connection.");
			}
			catch (Exception $e) {
				die($e->getMessage());
			}
		}
		
		// Execute database query
		function query($query) {
			try {
				$this->result = @mysql_query($query, $this->linkid);
				if (! $this->result) 
					throw new Exception("The database query failed.");
			}
			catch (Exception $e) {
				echo($e->getMessage);
			}
			$this->querycount++;
			return $this->result;
		}
		
		// Get information about the most recent query
		function queryInfo() {
			return @mysql_info($this->linkid); 
		}
		
		function getLinkID() {
			return $this->linkid; 
		}
		
		// Determine total rows affected by query.
		function affectedRows() {
			$count = @mysql_affected_rows($this->linkid);
			return $count;
		}
		
		// Determine total rows affected by the query.
		function numRows() {
			$count = @mysql_num_rows($this->result);
			return $count;
		}
		
		// Return query reslut row as an object.
		function fetchObject() {
			$row = @mysql_fetch_object($this->result);
			return $row;
		}
		
		// Return query result row as an indexed array.
		function fetchRow() {
			$row = @mysql_fetch_row($this->result);
			return $row;
		}
		
		// return query result row as an associative array.
		function fetchArray() {
			$row = @mysql_fetch_array($this->result);
			return $row;
		}
		
		// Return the total number of queries executed during the lifetime of this object.
		function numQueries() {
			return $this->querycount;
		}
		
		// Return the number of fields in a result set
		function numberFields() {
			return @mysql_num_fields($this->result);
		}
		
		// Return a field name given and integer offset
		function fieldName($offset) {
			return @mysql_field_name($this->result, $offset);
		}
		
		// Return a field length given and integer offset
		function fieldLength($offset) {
			return @mysql_field_len($this->result, $offset);
		}
		
		// Return a field type given and integer offset
		function fieldType($offset) {
			return @mysql_field_type($this->result, $offset);
		}
		
		function cut($text, $limit) {
			if (strlen($text) > $limit) {
				$text = substr($text, 0, $limit).'...';
			}
			return $text;
		}	
		
		// TABLE RENDERER
		function displayRecords($table='', $width='', $sort='id', $record_start=0, $variables='', $url='') {

			$counter = 0;
					
			if ($this->numRows() > 0) {
				
				$resultHTML .= '<table cellspacing="1" width="'.$width.'" id="table">'."\n";
				$resultHTML .= '<tr>'."\n";
				
				// HEADER ROW
				$fieldCount = $this->numberFields();
				$resultHTML .= '<th>edit</td>'."\n";
				$resultHTML .= '<th>delete</td>'."\n";
				if ($table != 'user') { $resultHTML .= '<th>copy</td>'."\n"; }
				
				for ($i = 0; $i < $fieldCount; $i++) {
					
					$rowName = $this->fieldName($i);
					
					if ($rowName == $sort) {
						$resultHTML .= '<th class="sort"><a href="'.$_SERVER['PHP_SELF'].'?table='.$table.'&sort='.$rowName.'&record_start='.$record_start.$variables.'">'.$rowName.'</a></td>'."\n";
					} else {
						$resultHTML .= '<th><a href="'.$_SERVER['PHP_SELF'].'?table='.$table.'&sort='.$rowName.'&record_start='.$record_start.$variables.'">'.$rowName.'</a></td>'."\n";
					}
					
				}
				
				$resultHTML .= '</tr>'."\n"; // CLOSE HEADER ROW
				
				while ($row = $this->fetchRow()) {
				
					$resultHTML .= '<tr>'."\n"; // OPEN RECORD ROW
					
					if ($counter % 2 != 0) {
					} else {
					}
					
					$resultHTML .= '<td><a href="../table/'.$table.'.php?id='.$row[1].'"><img src="../styles/default/edit.png" alt="edit" border="0" /></a></td>'."\n";
					$resultHTML .= '<td>'."<a href=\"javascript:deleteRecord('".$table."', ".$row[1].", '".$url."');\"".'><img src="../styles/default/delete.png" alt="delete" border="0" /></a></td>'."\n";
					if ($table != 'user') { $resultHTML .= '<td><a href="../table/'.$table.'.php?id='.$row[1].'&duplicate=1"><img src="../styles/default/copy.png" alt="duplicate" border="0" /></a></td>'."\n"; }
					for ($i = 0; $i < $fieldCount; $i++) {
					
						$rowName = $this->fieldName($i);	
						
						if (strlen($row[$i]) != 0) {
							if ($rowName == 'status') {
								if ($row[$i] == 'enabled') {
									$resultHTML .= '<td><a href="../panel/view.php?id='.$row[1].'&table='.$table.'&sort='.$sort.'&record_start='.$record_start.$variables.'&status=disabled"><img src="../styles/default/enabled.png" alt="disabled" border="0" /></a></td>'."\n";
								} else {
									$resultHTML .= '<td><a href="../panel/view.php?id='.$row[1].'&table='.$table.'&sort='.$sort.'&record_start='.$record_start.$variables.'&status=enabled"><img src="../styles/default/disabled.png" alt="enabled" border="0" /></a></td>'."\n";
								}
							} else {
								$resultHTML .= '<td>'.$row[$i].'</td>'."\n";
							}
						} else {
							$resultHTML .= '<td class="empty">empty</td>'."\n";
						}
					}
					$resultHTML .= '</tr>'."\n"; // CLOSE RECORD ROW
					$counter++;
				}
				
				$resultHTML .= '</table>'."\n";
				
			} else {
				$resultHTML = "Sorry, no results were found.";
			}
			
			return $resultHTML;
		}
		
	}
?>