Ver Mensaje Individual
  #2 (permalink)  
Antiguo 13/07/2004, 09:01
edwinmc
 
Fecha de Ingreso: octubre-2003
Mensajes: 152
Antigüedad: 20 años, 6 meses
Puntos: 2
Puedes analizar esta clase elaborada con la informañcion de varios ejemplos, aun no esta terminada pero ya funciona.

Código PHP:
<?php
    
//    ********************************************************************* //

    //        CLASES MANEJO DE BASE DE DATOS                                  //
    //        Por : Edwin Fredy Mamani Calderon                               //
    //        email : [email][email protected][/email]                                       //
    //        web : [url]http://www.gruposistemas.com[/url]                                //
    //                                                                        //
    //        CMysqlDatabase : Conecta la base de datos y Hace Consultas,     //
    //                         Con varia funciones definidas                  //
    //        CMysqlResult : Maneja Registros de acurdo a CMysqlDatabase      //

    // ********************************************************************** //

    
function fun_Error($mensaje)
    {
        echo 
"<font color='#990000'><em>".$mensaje."</em></font>";
    }

    
define("CONFIGURACION_MYSQL_HOST",$db_host);
    
define("CONFIGURACION_MYSQL_DB",$db_data);
    
define("CONFIGURACION_MYSQL_USER",$db_user);
    
define("CONFIGURACION_MYSQL_PASS",$db_pass);


    
/////////////  CLASE CMYSLDATABASE -> Conecta con la Base de datos

    
class CMysqlDatabase
    
{
        var 
$m_idConeccion;
        var 
$m_idConsulta;
        var 
$m_HOST;
        var 
$m_USER;
        var 
$m_PASS;
        var 
$m_DB;

        function 
Open($database="",$user="root",$pass="",$host="localhost")
        {
            
$this->m_DB $database;
            
$this->m_USER $user;
            
$this->m_PASS $pass;
            
$this->m_HOST $host;

            if(
$this->m_idConeccion == 0)
            {
                
$this->m_idConeccion mysql_connect($this->m_HOST,$this->m_USER,$this->m_PASS);
                if(!
$this->m_idConeccion)
                {
                     
fun_Error("No se puede acceder : (\"$this->m_HOST\",\"$this->m_USER\",password)");
                    
$this->printError();
                    exit;
                }

                if(!
mysql_select_db($this->m_DB,$this->m_idConeccion))
                {
                    
fun_Error("No se puede conectar con la base datos : ".$this->m_DB);
                    
$this->printError();
                    exit;
                }
            }
            return 
true;
        }

        function 
Conectar()
        {
            return 
$this->Open(CONFIGURACION_MYSQL_DB,CONFIGURACION_MYSQL_USER,CONFIGURACION_MYSQL_PASS,CONFIGURACION_MYSQL_HOST);
        }

        function 
Consultar($sql="")
        {
                
$this->m_idConsulta mysql_query($sql,$this->m_idConeccion);
                if(!
$this->m_idConsulta)
                {
                    
printError("<b>Error en la Consulta :</b><br><br>");
                    return 
false;
                }
                return 
true;
        }

        function 
getConsulta()
        {
            return 
$this->m_idConsulta;
        }

        function 
getConeccion()
        {
            return 
$this->m_idConeccion;
        }

        function 
printError()
        {
                    echo 
"<br><br><b>Mysql Error</b> : ".mysql_errno($this->m_idConeccion)." (".mysql_error($this->m_idConeccion).") <br>\n";
        }

        function 
Insert($table,$campos="",$valores)
        {
            if(
substr_count($valores,"'")<=0)
            {
                    
$valores=addslashes($valores);

                    
$valores=str_replace(",","','",$valores);
                    
$valores "'".$valores."'";
            }

            if(empty(
$campos))
                
$sql "INSERT INTO $table VALUES($valores)";
            else
                
$sql "INSERT INTO $table($campos) VALUES($valores)";
//            echo $sql;
            
return $this->Consultar($sql);
        }
        function 
Update($table,$campos,$valores,$condicion)
        {
            
$valores str_replace("','","'_,_'",$valores);
            
            
$campos split(",",$campos);
            
$valores split("_,_",$valores);

            
$numcampos count($campos);
    
            
$sql "UPDATE $table SET ";
            for(
$i=0;$i<$numcampos;$i++)
            {
                if(
$i!=0)
                {
                    
$sql.=",";
                }
                
$sql.= $campos[$i]."=".$valores[$i];
            }
            
$sql.= " WHERE $condicion";
            
            return 
$this->Consultar($sql);
        }

        function 
Delete($table,$condicion="")
        {
            
$sql "DELETE FROM $table";
            if(!empty(
$condicion))
            {
                
$sql.= " WHERE $condicion";
            }
            return 
$this->Consultar($sql);
        }
    }


    
//////////////// CMYSQLRESULT -> Maneja los Registros de una consulta

    
class CMysqlResult
    
{
        var 
$m_Result;
        var 
$m_Rows;
        var 
$m_Cols;
        var 
$m_currentRow;
        var 
$m_Registro = array();

        function 
Open($cmysqldatabase)
        {
            
$this->m_Result $cmysqldatabase->getConsulta();
            
$this->m_Rows mysql_num_rows($this->m_Result);
            
$this->m_currentRow 0;
            
//$this->m_Registro = mysql_fetch_array($this->m_Result);
            
$m_Cols mysql_num_fields($this->m_Result);
        }

        function 
fetchArray()
        {
            if(
$this->m_currentRow<$this->m_Rows)
            {
                
$this->m_Registro mysql_fetch_array($this->m_Result);
                
$this->m_currentRow++;
                return 
true;
            }
            return 
false;
        }

        function 
moveFirst()
        {
            
$this->m_currentRow=0;
            
mysql_data_seek($this->m_Result,$this->m_currentRow);
            return 
true;
        }

        function 
moveLast()
        {
            
$this->m_currentRow $this->m_Rows-1;
            
mysql_data_seek($this->m_Result,$this->m_currentRow);
            
$this->m_Registro mysql_fetch_array($this->m_Result);
            return 
true;
        }

        function 
getDato($campo)
        {
            return 
stripslashes($this->m_Registro[$campo]);
        }
        function 
getDatoRow($row,$campo)
        {
            return 
stripslashes(mysql_result($this->m_Result,$row,$campo));
        }

        function 
getRows()
        {
            return 
$this->m_Rows;
        }
        function 
getCurrentRow()
        {
            return 
$this->m_currentRow;
        }
    }
?>
__________________
Mamani Calderón, Edwin Fredy
Ingeniería de Sistemas www.gruposistemas.com