<?php

	class MyDBLayer {

		private $db = '';				// Base de datos.
		private $host = 'localhost';	// Servidor.
		private $user = 'user';			// Usuario.
		private $password = 'password';	// Password.

		public $handler = null;			// Handler de la conexión.

		private $engine = 'mysql';		// Motor de base de datos.
		private $autoconnect = false;	// Indica si debemos autoconectar al crear el objeto.
		private $connected = false;		// Indica si estamos conectados.

		public $queries = array();		// Contiene los queries ejecutados.

		public function __construct( $db, $host, $user = '', $password = '', $connect = false, $engine = 'mysql' ) {

			//
			// Inicializamos variables
			//

			var $tmp = array();

			$this->db = $db;
			$this->host = $host;
			if (strpos($host,'@') !== false) {
				$tmp = explode( '@', $host );
				$this->host = $tmp[0];
				$this->user = $tmp[1];
				if (count($tmp) > 2) {
					$this->password = $tmp[2];
				}
				if (count($tmp) > 3) {
					$this->autoconnect = (in_array($tmp[4],array('yes','true','1') ? true : false;
				}
			} else if (is_array($host)) {
				$this->host = $host[0];
				$this->user = $host[1];
				$this->password = $host[2];
				if (count($host) > 3) {
					$this->autoconnect = (in_array($host[4],array('yes','true','1') ? true : false;
				}
			} else {
				$this->user = $user;
				$this->password = $password;
				$this->autoconnect = $connect;
			}

			if ($engine != 'mysql') $this->engine = $engine;

			if ($this->autoconnect) {	// Si debemos conectar al inicio lo hacemos...
				$this->connect();
			}

		}

		public function connect() {

			if ($this->engine == 'mysql') {
				$this->handler = @mysql_connect( $this->host, $this->user, $this->password );
			}

			if ($this->handler) {
				if ($this->engine == 'mysql') {
					$result = mysql_select_db($this->db, $this->handler);
				}
				if (!$result) {
					$this->errors[] = 'Error de selección de base de datos: ' . $this->showerror();
					return false;
				}
				$this->connected = true;
				return true;
			} else {
				$this->errors[] = 'Error de conexión con el servidor: ' . $this->showerror();
				return false;
			}

		}

		public function query( $sql, $query = 'main' ) {

			if (!$this->connected) {		// Comprobamos si estamos conectados, si no lo hacemos.
				$this->connect();
			}

			if ($this->connected) {

				if ($this->engine == 'mysql') {

					//
					// Realizamos el query y almacenamos el resultado en el array "queries"
					//
					$this->queries[$query]['query'] = $sql;
					$this->queries[$query]['result'] = mysql_query( $this->queries[$query]['query'], $this->db ) || $this->SendError();

					if ($this->queries[$query]['result']) {

						//
						// Si el query ha tenido éxito, extraemos información del resultado.
						//
						$this->queries[$query]['recno'] = 0;

						if ((substr( strtoupper(trim($this->queries[$query]['query'])), 0, 6 ) == "SELECT") || (substr( strtoupper(trim($this->queries[$query]['query'])), 0, 7 ) == "EXPLAIN") || (substr( strtoupper(trim($this->queries[$query]['query'])), 0, 4 ) == "SHOW")) {

							$this->queries[$index]['numfields'] = mysql_num_fields( $this->queries[$index]['result'] );
							$this->queries[$index]['numrows']   = mysql_num_rows( $this->queries[$index]['result'] );

							for ($ct=0;$ct<$this->queries[$index]['numfields'];$ct++) {
								$this->queries[$index]['fields'][$ct]['name'] = mysql_field_name( $this->result, $ct );
								$this->queries[$index]['fields'][$ct]['type'] = mysql_field_type( $this->result, $ct );
								$this->queries[$index]['fields'][$ct]['size'] = mysql_field_len( $this->result, $ct );
							}

						}

					}

				}

				return $this->queries[$index]['result'];

			}

		}

		public function get( $row, $col, $query = 'main' ) {

			$return = false;

			if ($this->engine == "mysql") {
				$return = mysql_result( $this->queries[$query]['result'], $row, $col );
			}

			return $return;

		}

		public function getrow( $row = 0, $query = 'main' ) {

			$return = false;

			for ($ct=0;$ct<$this->queries[$query]['numfields'];$ct++) {
				$return[$ct] = $this->get($row, $ct);
			}

			return $return;

		}

		public function getassoc( $row = 0, $query = 'main' ) {

			$return = false;

			for ($ct=0;$ct<$this->queries[$query]['numfields'];$ct++) {
				$return[$this->queries[$query]['fields'][$ct]['name']] = $this->get($row, $ct);
			}

			return $return;

		}

		public function getarrayassoc( $start = 0, $end = 0, $query = 'main' ) {

			$return = false;

			if ($end == 0) {
				$end = $this->queries[$query]['numrows'];
			}

			for ($ctr=$start;$ctr<=$end;$ctr++) {
				$return[$ctr] = $this->getassoc($ctr, $query);
			}

			return $return;

		}

		public function insert( $table, $fields, $values = false, $query = 'main' ) {

			$return = false;

			if ($table !== false) {
				if (!$values) {
					$qry = 'INSERT INTO ' . $table . ' VALUES ( "' . implode( '", "', $fields ) . '" )';
				} else {
					$qry = 'INSERT INTO ' . $table . ' ( "' . implode( '", "', $fields ) . '" )' . ' VALUES ( "' . implode( '", "', $values ) . '" )';
				}
				$return = $this->query( $qry, $query )
			}

			return $return;

		}

		public function update( $table, $fields, $values, $where = '1=1', $query = 'main' ) {

			$return = false;

			if ($table !== false) {
				$qry = 'UPDATE ' . $table . ' SET ';
				$tot = count( $fields );
				for ($ct=0;$ct<$tot;$ct++) {
					if ($ct > 0) $qry .= ', ';
					$qry .= $fields[$ct] . '=' . '"' . $values[$ct] . '"';
				}
				$qry .= ' ' . $where;
				$return = $this->query( $qry, $query )
			}

			return $return;

		}

		public function delete( $table, $where = '1=1', $query = 'main' ) {

			$return = false;

			if ($table !== false) {
				$qry = 'DELETE FROM ' . $table . ' ' . $where;
				$return = $this->query( $qry, $query );
			}

			return $return;

		}

		private function showerror() {
			if ($this->engine == 'mysql') {
				return mysql_error();
			}
		}

	}

?>