Ver Mensaje Individual
  #5 (permalink)  
Antiguo 01/10/2009, 13:51
Nisrokh
 
Fecha de Ingreso: septiembre-2009
Ubicación: Neuquén
Mensajes: 142
Antigüedad: 14 años, 7 meses
Puntos: 12
Respuesta: Programación orientada a ojetos

Ya la verdad que conviene... Ademas hace los codigos mucho mas limpios...

Y algo bueno que tiene, tambien es lo de hacer classes reutilizables, yo me manejo haciendo clases bien hechas, e independientes... Ya que hago paginas web, y cuando varios cleintes me piden por ej. pequeñas aplicaciones con MySQL, yo ya me hice una pequeña y simple libreria para el manejo de base de datos, entonces se me hace mas facil, y no tengo que andar perdiendo el tiempo volviendo a crear tantos codigos nuevamente...

Aca te la dejo para que la mires si queres... Es algo simpl,e no muy compleja, pero hasta el momento me ha sido efectiva... De a poco cuando tengo tiempo trato de mejorarla, y tambien mis otras librerias...

Código php:
Ver original
  1. <?php
  2.  
  3. /**
  4.  * @author estilodg.com <[email protected]>
  5.  * @copyright 2009
  6.  */
  7.  
  8. /**
  9.  * MySQL Management Class
  10.  *
  11.  * @author Diego P. M. Baltar <www (dor) estilodg (dot) com>
  12.  * @package SimpleMySQL
  13.  */
  14. class SimpleMySQL
  15. {
  16.     /**
  17.      * Link identifier
  18.      *
  19.      * @access public
  20.      * @var resource
  21.      */
  22.     public $link = null;
  23.    
  24.     /**
  25.      * Selected database
  26.      *
  27.      * @access public
  28.      * @var string
  29.      */
  30.     public $database = null;
  31.    
  32.     /**
  33.      * Query result resource
  34.      *
  35.      * @access public
  36.      * @var resource
  37.      */
  38.     public $result = null;
  39.    
  40.     /**
  41.      * Query result data
  42.      *
  43.      * @access public
  44.      * @var array
  45.      */
  46.     public $query_result = array();
  47.    
  48.     /**
  49.      * Query error
  50.      *
  51.      * @access public
  52.      * @var boolean
  53.      */
  54.     public $query_error = false;
  55.    
  56.     /**
  57.      * Selected rows
  58.      *
  59.      * @access public
  60.      * @var integer
  61.      */
  62.     public $rows = null;
  63.    
  64.     /**
  65.      * Number of affected rows
  66.      *
  67.      * @access public
  68.      * @var integer
  69.      */
  70.     public $affected_rows = null;
  71.    
  72.     /**
  73.      * Last inserted id
  74.      *
  75.      * @access public
  76.      * @var integer
  77.      */
  78.     public $last_id = null;
  79.    
  80.     /**
  81.      * MySQL connection state
  82.      *
  83.      * @access public
  84.      * @var boolean
  85.      */
  86.     public $ready = false;
  87.    
  88.     /**
  89.      * Database tables
  90.      */
  91.     private $tables = array();
  92.    
  93.     public function __construct($hostname = 'localhost', $username = 'root', $password = '')
  94.     {
  95.         return $this->connect($hostname, $username, $password);
  96.     }
  97.    
  98.     public function __destruct()
  99.     {
  100.         return true;
  101.     }
  102.    
  103.     /**
  104.      * MySQL connect
  105.      *
  106.      * @param string $hostname MySQL server address/ip(port)
  107.      * @param string $username MySQL username
  108.      * @param string $password MySQL password
  109.      * @return boolean
  110.      */
  111.     private function connect($hostname,$username,$password)
  112.     {
  113.         $this->link = @mysql_connect($hostname, $username, $password, true);
  114.         if (!$this->link) return exit(mysql_error($this->link));
  115.         else return $this->ready = true;
  116.     }
  117.    
  118.     /**
  119.      * MySQL select database
  120.      *
  121.      * @param string $database MySQL database name to use
  122.      * @return boolean
  123.      */
  124.     public function select_db($database = '')
  125.     {
  126.         if (!$this->link) return exit(mysql_error($this->link));
  127.         else if (!@mysql_select_db($database)) {
  128.             $this->ready = false;
  129.             return exit(mysql_error($this->link));
  130.         }
  131.         else $this->database = $database;
  132.         return true;
  133.     }
  134.    
  135.     /**
  136.      * MySQL query
  137.      *
  138.      * @param string $sentence MySQL query sentence
  139.      * @return integer Number of selected rows
  140.      */
  141.     public function query($sentence = '')
  142.     {
  143.         if (!$this->link) return exit(mysql_error($this->link));
  144.         $this->result = @mysql_query($sentence, $this->link);
  145.         if (!$this->result) {
  146.             $this->query_error = true;
  147.             return exit(mysql_error($this->link));
  148.         }
  149.        
  150.         $this->affected_rows = mysql_affected_rows($this->link);
  151.        
  152.         if (preg_match('/^\s*(insert|replace)(.+)/is',$sentence)) $this->last_id = mysql_insert_id($this->link);
  153.        
  154.         if (preg_match('/^\s*(select|replace)(.+)/is',$sentence))
  155.         {
  156.             $rows = 0;
  157.             while ($row = mysql_fetch_object($this->result)) {
  158.                 $this->query_result[$rows] = $row;
  159.                 ++$rows;
  160.             }
  161.         }
  162.        
  163.         @mysql_free_result($this->result);
  164.        
  165.         if ($this->query_error) $this->query_error = false;
  166.        
  167.         $this->rows = $rows;
  168.         return true;
  169.     }
  170.    
  171.     /**
  172.      * Clean cached query result
  173.      *
  174.      * @access public
  175.      * @return void
  176.      */
  177.     public function clean()
  178.     {
  179.         $this->query_error = false;
  180.         $this->query_result = array();
  181.         $this->affected_rows = null;
  182.     }
  183.    
  184.     /**
  185.      * Espaces a string
  186.      *
  187.      * @access public
  188.      * @param string $string
  189.      * @return string
  190.      */
  191.     public function escape($string)
  192.     {
  193.         if (!$this->link) return exit(mysql_error($this->link));
  194.         $string = stripslashes($string);
  195.         return @mysql_real_escape_string($string, $this->link) ;
  196.     }
  197. }
  198.  
  199. ?>

Documento en ingles porque el ingles siempre es mas corto que escribir que el español jeje

Saludos.