Ver Mensaje Individual
  #44 (permalink)  
Antiguo 11/11/2010, 17:39
Avatar de carlos_belisario
carlos_belisario
Colaborador
 
Fecha de Ingreso: abril-2010
Ubicación: Venezuela Maracay Aragua
Mensajes: 3.156
Antigüedad: 14 años, 1 mes
Puntos: 461
Respuesta: lo estoy haciendo bien??

aca les dejo los metodos que use para trabajar el sql

Código PHP:
Ver original
  1. <?php
  2.  
  3.    #######################################################
  4.  
  5.    #   Clace para conexion con db                        #
  6.  
  7.    #                                                     #
  8.  
  9.    #   Creado por Carlos Belisario               #
  10.  
  11.    #                                                     #
  12.  
  13.    #######################################################
  14.  
  15.  class db
  16.  
  17. {
  18.  
  19.     protected $_conect;
  20.  
  21.     protected $_query;
  22.  
  23.     protected $_row;
  24.  
  25.     protected $_result;
  26.  
  27.     public function __construct()
  28.  
  29.     {
  30.  
  31.         $site_path = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'config.ini';
  32.  
  33.         $this->_conect=DatabaseFactory::create($site_path);
  34.  
  35.     }
  36.  
  37.     public function selectAll($table,$condicion=array()){
  38.  
  39.         $result=array();
  40.  
  41.         try{           
  42.  
  43.             if(empty($condicion))
  44.  
  45.             {
  46.  
  47.                 $this->_query=$this->_conect->prepare("SELECT * FROM $table");
  48.  
  49.                 $this->_query->execute();
  50.  
  51.             }
  52.  
  53.             else{
  54.  
  55.                 foreach($condicion as $key=>$value){
  56.  
  57.                     $valorCondicion[]="$key='".$value."'";
  58.  
  59.                 }          
  60.  
  61.                 $condiciones=implode(" AND ",$valorCondicion);
  62.  
  63.                 $sql="SELECT * FROM $table WHERE $condiciones";
  64.  
  65.                 $this->_query=$this->_conect->prepare($sql);
  66.  
  67.                 $this->_query->execute();
  68.  
  69.             }  
  70.  
  71.             while($this->_row=$this->_query->fetch()){ 
  72.  
  73.                 $result[]=$this->_row;         
  74.  
  75.             }          
  76.  
  77.             return $result;
  78.  
  79.         }
  80.  
  81.         catch(PDOExeption $e){
  82.  
  83.             echo $e->getMessaage();
  84.  
  85.         }
  86.  
  87.     }  
  88.  
  89.     public function ejecutarSentencia($query,$parametro=NULL,$return=false )
  90.  
  91.     {
  92.  
  93.         $result=array();
  94.  
  95.         try{       
  96.  
  97.             $this->_query=$this->_conect->prepare($query);
  98.  
  99.             $this->_query->execute($parametro);
  100.  
  101.             if($return==true){                                 
  102.  
  103.                 while($this->_row=$this->_query->fetch()){ 
  104.  
  105.                     $result[]=$this->_row;
  106.  
  107.                 }              
  108.  
  109.                 return $result;
  110.  
  111.             }          
  112.  
  113.         }
  114.  
  115.         catch(PDOExeption $e){
  116.  
  117.             echo $e->getMessaage();
  118.  
  119.         }
  120.  
  121.     }      
  122.  
  123.     public function ejecutarSentencia1($query,$parametro=NULL,$return=false )
  124.  
  125.     {
  126.  
  127.         $result=array();
  128.  
  129.         try{       
  130.  
  131.             $this->_query=$this->_conect->prepare($query);
  132.  
  133.             $this->_query->execute($parametro);
  134.  
  135.             if($return==true){                                 
  136.  
  137.                 while($this->_row=$this->_query->fetch()){ 
  138.  
  139.                     return $this->_row;
  140.  
  141.                 }              
  142.  
  143.                 return $result;
  144.  
  145.             }          
  146.  
  147.         }
  148.  
  149.         catch(PDOExeption $e){
  150.  
  151.             echo $e->getMessaage();
  152.  
  153.         }
  154.  
  155.     }      
  156.  
  157.     public function insert($table,$campos,$valores)
  158.  
  159.     {
  160.  
  161.         if(!is_array($campos) || !is_array($valores)){
  162.  
  163.             echo "Los datos deben de estar en formato de arreglo";
  164.  
  165.         }
  166.  
  167.         try{                       
  168.  
  169.             $campo=implode(",",$campos);           
  170.  
  171.             foreach($valores as $id=>$valor){
  172.  
  173.                 $valorIngresar[]="'".$valor."'";
  174.  
  175.             }
  176.  
  177.             $valors=implode(",",$valorIngresar);
  178.  
  179.             $sql="INSERT INTO $table ($campo) VALUES ($valors)";           
  180.  
  181.             $this->_conect->exec($sql);        
  182.  
  183.         }
  184.  
  185.         catch(PDOExeption $e){
  186.  
  187.             echo $e->getMessaage();
  188.  
  189.         }  
  190.  
  191.     }  
  192.  
  193.     public function update($table,$valores,$condicion)
  194.  
  195.     {
  196.  
  197.         if(!is_array($valores)){
  198.  
  199.             echo "Los datos deben de estar en formato de arreglo";
  200.  
  201.         }
  202.  
  203.         try{           
  204.  
  205.             foreach($valores as $id=>$valor){
  206.  
  207.                 $valorIngresar[]="$id='".$valor."'";
  208.  
  209.             }
  210.  
  211.             foreach($condicion as $key=>$value){
  212.  
  213.                 $de=explode(".",$value);
  214.  
  215.                 if(count($de)>1){
  216.  
  217.                     $valorCondicion[]="$key=".$value;
  218.  
  219.                 }
  220.  
  221.                 else{
  222.  
  223.                     $valorCondicion[]="$key='".$value."'";
  224.  
  225.                 }
  226.  
  227.             }          
  228.  
  229.             $valors=implode(",",$valorIngresar);                       
  230.  
  231.             $condiciones=implode(" AND ",$valorCondicion);
  232.  
  233.             $sql="UPDATE $table SET $valors WHERE $condiciones";
  234.  
  235.             $this->_conect->exec($sql);        
  236.  
  237.         }
  238.  
  239.         catch(PDOExeption $e){
  240.  
  241.             echo $e->getMessaage();
  242.  
  243.         }  
  244.  
  245.     }
  246.  
  247.     public function deleteAll($table,$return=false)
  248.  
  249.     {
  250.  
  251.         try{           
  252.  
  253.             $this->_result=$this->_conect->exec("DELETE FROM $table");         
  254.  
  255.             if($return){
  256.  
  257.                 return $this->_result;
  258.  
  259.             }              
  260.  
  261.         }
  262.  
  263.         catch(PDOExeption $e){
  264.  
  265.             echo $e->getMessaage();
  266.  
  267.         }
  268.  
  269.     }
  270.  
  271.     public function reiniciarTabla($table){
  272.  
  273.         try{           
  274.  
  275.             $this->_result=$this->_conect->exec("TRUNCATE TABLE $table");              
  276.  
  277.         }
  278.  
  279.         catch(PDOExeption $e){
  280.  
  281.             echo $e->getMessaage();
  282.  
  283.         }
  284.  
  285.     }
  286.  
  287. }

si se fijan en la archivo dbfactory se incluye el patron registry utilice el creado en este articulo espero le sirva a alguien saludos
__________________
aprende d tus errores e incrementa tu conocimientos
it's not a bug, it's an undocumented feature By @David
php the right way