Ver Mensaje Individual
  #5 (permalink)  
Antiguo 09/10/2009, 05:28
Nisrokh
 
Fecha de Ingreso: septiembre-2009
Ubicación: Neuquén
Mensajes: 142
Antigüedad: 15 años, 5 meses
Puntos: 12
Respuesta: Simplificar consulta a MySQL

Bueno, mira, te muestro como facilita mucho el uso de la clase que yo utilizo para apps. simples...

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

Ahora te voy a mostrar un ejemplo de como la utilizo, desde la conexion hasta obtener datos de alguna tabla, por ej. de una tipica tabla de noticias:

noticias [ id, titulo, fecha, contenido ]

Código php:
Ver original
  1. <?php
  2.  
  3. // Crear el objecto (conectandose a la base de datos)
  4. $SimpleMySQL = new SimpleMySQL('localhost', 'root', 'pasword');
  5.  
  6. // Seleccionar la base de datos
  7. $SimpleMySQL->select_db('base_de_datos');
  8.  
  9. // Ejecutar consulta MySQl
  10. $SimpleMySQL->query("SELECT * FROM noticias");
  11.  
  12. // Devolver mensaje de error si no hay resultados
  13. if ($SimpleMySQL->affected_rows < 1) return print 'No hay contenidos de noticias';
  14. else {
  15.         // $SimpleMySQL->query_result es un array con los resultados
  16.         // traidos con mysql_fetch_object()
  17.         foreach ($SimpleMySQL->query_result as $row) {
  18.                 print '<h1>' .$row->titulo. '. ' .$row->titulo. '</h1>';
  19.                 print '<h4>' .$row->fecha. '</h4>';
  20.                 print '<div>' .$row->contenido. '</div>';
  21.         }
  22. }
  23.  
  24. // Limpiar resultados para poder ejecutar nuevas consultas
  25. $SimpleMySQL->clean();
  26.  
  27. ?>

Espero que te sea util, saludos.