Location: PHPKode > scripts > Pablosky MySQL > pablosky-mysql/class.pablosky_mysql.php
<?php

	class pablosky_mysql
	{
		// pablosky_mysql
		// 11 Feb 2007
		// MYSQL very easy access wrapper using mysqli extend and exceptions
		// Required PHP 5 
		//
		// Methods
		// get_results: Returns an object with the list of records answering to the SQL sentence. (Select)
		// get_record: Returns an object with one record answering to the SQL sentence. Returns a records only (Select)
		// set_query: Execute sentence and returns ID of auto_increment field of table used or zero. (Insert, Update.)
		// get_num_rows: Returns the number of results of the last SQL sentence.
		// get_num_fields: Returns the number of fields of the last SQL sentence.
		// close: Close the DB conexion.
		//
		// License: free for no-comercial use.
		//
		// Author: Pablo Alba Rico (hide@address.com)
		
		// propiedades
		private $db;
		private $row;
		private $result;
		private $last_result = array();
		
		// ===================================================================================================
		// construct
		// PUBLIC
		// param $db_user - (string) DB user name
		// param $db_password - (string) DB user passwordd
		// param $db_name - (string) DB name
		// param $db_host - (string) DB host
		function __construct ($db_host, $db_user, $db_password, $db_name)
		{
			@ $this->db = new mysqli($db_host, $db_user, $db_password, $db_name);
			
			if (mysqli_connect_errno())
			{
				throw new Exception ('Error al conectar con la base de datos.');
			}
		}
		// Connects to the server and selects a database
		// ===================================================================================================
		
		
		// ===================================================================================================
		// destruct
		// PUBLIC
		function __destruct()
		{
			$this->close();
		}
		// Close DB Connection
		// ===================================================================================================
		
		
		// ===================================================================================================
		// exectute_query
		// PRIVATE
		// param $sql - (string) sentence SQL
		private function execute_query ($sql)
		{
			if (!$sql)
			{
				throw new Exception ('Cadena SQL vacia.');
			}
						
			$this->result = $this->db->query ($sql);
			
			if (!$this->result)
			{
				throw new Exception ('Error al ejecutar la sentencia SQL.');
			}
			
			return $this->result;
		}
		// Execute SQL sentence
		// ===================================================================================================
		
		
		// ===================================================================================================
		// get_results
		// PUBLIC
		// param $sql - (string) sentence SQL
		public function get_results ($sql)
		{
			$this->execute_query ($sql);
			
			for ($i = 0; $i < $this->result->num_rows; $i++)
			{
				$this->row = $this->result->fetch_object();
				
				$this->last_result[$i] = $this->row;
			}
			
			// Returns an object with the list of records answering to the SQL sentence.
			return $this->last_result;
			
			$this->result->free();
		}
		// Returns an object with the list of records answering to the SQL sentence. (Select)
		// ===================================================================================================
		
		
		// ===================================================================================================
		// get_record
		// PUBLIC
		// param $sql - (string) sentence SQL
		public function get_record ($sql)
		{
			$this->execute_query ($sql);
			
			$this->row = $this->result->fetch_object();
			
			// Return an object with record answering to the SQL sentence.
			return $this->row;
			
			$this->result->free();
		}
		// Returns an object with record answering to the SQL sentence. Returns a records only (Select)
		// ===================================================================================================
		
		
		// ===================================================================================================
		// set_query
		// PUBLIC
		// param $sql - (string) sentence SQL
		public function set_query ($sql)
		{
			$this->execute_query ($sql);
			
			// devuelve ID del campo con auto_increment, si lo hay
			return $this->db->insert_id;
			
			$this->result->free();
		}
		// Execute sentence and returns ID of auto_increment field of table used or zero. (Insert, Update.)
		// ===================================================================================================
		
		
		// ===================================================================================================
		// get_num_rows
		// PUBLIC
		public function get_num_rows ()
		{
		
			return $this->result->num_rows;
		}
		// Devuelve el número de resultados que ha devuelto la última sentencia SQL.
		// ===================================================================================================
		
		
		// ===================================================================================================
		// get_num_fields
		// PUBLIC
		public function get_num_fields ()
		{
			return $this->result->field_count;
		}
		// Returns the number of fields of the last SQL sentence.
		// ===================================================================================================
		
		
		// ===================================================================================================
		// close
		// PUBLIC
		public function close ()
		{
			$this->db->close();
		}
		// Close the DB conexion.
		// ===================================================================================================
		
	}
	
?>
Return current item: Pablosky MySQL