bbdd_ip = $ip; $this->bbdd_usuario = $usuario; $this->bbdd_password = $password; $this->bbdd = $bbdd; $this->Errno = 0; $this->Error = ""; $this->id_conexion = 0; } /* Autor: Miguel Matas Oliva */ /* Fecha Creación: 05/11/2004 */ /* Fecha Última Modificación: 16/11/2004 */ /* Descripción: Crea una conexión a una base de datos. */ /* Respuesta: Si conecta correctamente retorna una id de conexión. En caso contrario retorna cero. */ /* Parámetros: No utiliza parámetros, usa las propiedades de la clase inicializadas en el constructor */ function Conectar() { // Conectamos al servidor $this->id_conexion = @mysql_connect ( $this->bbdd_ip, $this->bbdd_usuario, $this->bbdd_password); if ( !$this->id_conexion ) { $this->Error = "No se ha podido conectar a la BBDD." . "
" . "Error: " . mysql_error(); return 0; } // seleccionamos la base de datos if ( !@mysql_select_db ( $this->bbdd, $this->id_conexion ) ) { $this->Error = "Imposible Abrir la BBDD: " . $this->bbdd . "
" . "Error: " . mysql_error(); return 0; } /* Si hemos tenido éxito conectando devuelve el identificador de la conexión, sino devuelve 0 */ return $this->id_conexion; } /* Autor: Miguel Matas Oliva */ /* Fecha Creación: 05/11/2004 */ /* Fecha Última Modificación: 16/11/2004 */ /* Descripción: Desconecta de una base de datos a la que estemos cnoectados */ /* Respuesta: Si ha habido error desconectando retorna false. En caso de éxito retorna true */ /* Parámetros: No utiliza parámetros, usa las propiedades de la clase inicializadas en el constructor */ function Desconectar() { if ( !@mysql_close ( $this->id_conexion ) ) { $this->Errno = mysql_errno(); $this->Error = "Imposible Desconectar de la BBDD: " . $this->bbdd; return false; } return true; } /* Retorna la id de la conexion actual */ function GetIdConexion () { return $this->id_conexion; } /* Retorna la primera ocurrencia de un select */ function GetFirst ( $sql ) { $Id = mysql_query ( $sql , $this->id_conexion ); if ( !$Id ) { $this->Errno = mysql_errno(); $this->Error = "Ha habído un error en la Consulta SQL" . "
" . "Consulta: " .$sql . "
" . "Error: " . mysql_error(); echo $this->Error; return 0; } else { $rs = mysql_fetch_row ( $Id ); return $rs [ 0 ]; } } /* Autor: Miguel Matas Oliva */ /* Fecha Creación: 29/03/2005 */ /* Fecha Última Modificación: 29/03/2005 */ /* Descripción: Retorna la id más grande que se encuentre en una tabla */ function GetLastId ( $Tabla , $CampoID ) { $sql = "select max(" . $CampoID . ") from " . $Tabla; $Id = mysql_query ( $sql , $this->id_conexion ); if ( !$Id ) { $this->Errno = mysql_errno(); $this->Error = "Ha habído un error en la Consulta SQL" . "
" . "Consulta: " .$sql . "
" . "Error: " . mysql_error(); echo $this->Error; return 0; } else { $rs = mysql_fetch_row ( $Id ); return $rs [ 0 ]; } } } //fin de la clase BBDD_mysql /* Autor: Miguel Matas Oliva */ /* Fecha Creación: 04/12/2004 */ /* Fecha Última Modificación: 04/12/2004 */ class SQL { var $Tabla; var $ListaCampos; var $ListaValores; var $MuestraError; var $SqlQuery; var $Errno; var $Error; var $IdConexion; function SQL () { } function Push ( $Campo , $Valor ) { array_push ( $this->ListaCampos , $Campo ); array_push ( $this->ListaValores , $this->FiltroSQL($Valor) ); } function MontaSQL () { // Redefinida por los hijos } function Exec () { // Redefinida por los hijos } // filtra un string para que no contenga caracteres peligrosos para una transaccion sql function FiltroSQL ( $texto ) { $traductor = array("'" => "", "%" => ""); return strtr ( $texto , $traductor ); } } // fin de la clase SQL /* Autor: Miguel Matas Oliva */ /* Fecha Creación: 04/12/2004 */ /* Fecha Última Modificación: 04/12/2004 */ class SQLUpdate extends SQL { var $ListaCamposCondicion; var $ListaValoresCondicion; function SQLUpdate ( $Tabla , $IdConexion , $MuestraError = 0 ) { $this->Tabla = $Tabla; $this->MuestraError = $MuestraError; $this->SqlQuery = ""; $this->Errno = 0; $this->Error = ""; $this->IdConexion = $IdConexion; $this->ListaCampos = array (); $this->ListaValores = array (); $this->ListaCamposCondicion = array (); $this->ListaValoresCondicion = array (); } function PushCondicion ( $CampoCondicion , $ValorCondicion ) { array_push ( $this->ListaCamposCondicion , $CampoCondicion ); array_push ( $this->ListaValoresCondicion , $this->FiltroSQL($ValorCondicion) ); } function MontaSQL () { $top = count ( $this->ListaCampos ) - 1; $this->SqlQuery = ""; $this->SqlQuery = "update " . $this->Tabla . " set " ; for ( $i = 0 ; $i <= $top ; $i++ ) { $this->SqlQuery = $this->SqlQuery . $this->ListaCampos [ $i ] . " = '" . $this->ListaValores [ $i ] . "'"; if ( $i <> $top ) $this->SqlQuery = $this->SqlQuery . " , "; } $top = count ( $this->ListaCamposCondicion ) - 1; $this->SqlQuery = $this->SqlQuery . " where "; for ( $i = 0 ; $i <= $top ; $i++ ) { $this->SqlQuery = $this->SqlQuery . $this->ListaCamposCondicion [ $i ] . " = '" . $this->ListaValoresCondicion [ $i ] . "'"; if ( $i <> $top ) $this->SqlQuery = $this->SqlQuery . " and "; } } function Exec () { $this->MontaSQL(); if ( !mysql_query ( $this->SqlQuery , $this->IdConexion ) ) { $this->Errno = mysql_errno(); $this->Error = "Ha habído un error en el Update SQL" . "
" . "Query: " . $this->SqlQuery . "
" . "Error: " . mysql_error(); if ( $this->MuestraError ) echo $this->Error; } } } // fin de la clase SQLUpdate /* Autor: Miguel Matas Oliva */ /* Fecha Creación: 04/12/2004 */ /* Fecha Última Modificación: 04/12/2004 */ class SQLDelete extends SQL { function SQLDelete ( $Tabla , $IdConexion , $MuestraError = 0 ) { $this->Tabla = $Tabla; $this->MuestraError = $MuestraError; $this->SqlQuery = ""; $this->Errno = 0; $this->Error = ""; $this->IdConexion = $IdConexion; $this->ListaCampos = array (); $this->ListaValores = array (); } function MontaSQL () { $top = count ( $this->ListaCampos ) - 1; $this->SqlQuery = ""; $this->SqlQuery = "delete from " . $this->Tabla . " where " ; for ( $i = 0 ; $i <= $top ; $i++ ) { $this->SqlQuery = $this->SqlQuery . $this->ListaCampos [ $i ] . " = '" . $this->ListaValores [ $i ] . "'"; if ( $i <> $top ) $this->SqlQuery = $this->SqlQuery . " and "; } } function Exec () { $this->MontaSQL(); if ( !mysql_query ( $this->SqlQuery , $this->IdConexion ) ) { $this->Errno = mysql_errno(); $this->Error = "Ha habído un error en el Delete SQL" . "
" . "Query: " . $this->SqlQuery . "
" . "Error: " . mysql_error(); if ( $this->MuestraError ) echo $this->Error; } } } // fin de la clase SQLDelete /* Autor: Miguel Matas Oliva */ /* Fecha Creación: 04/12/2004 */ /* Fecha Última Modificación: 04/12/2004 */ class SQLInsert extends SQL { function SQLInsert ( $Tabla , $IdConexion , $MuestraError = 0 ) { $this->Tabla = $Tabla; $this->MuestraError = $MuestraError; $this->SqlQuery = ""; $this->Errno = 0; $this->Error = ""; $this->IdConexion = $IdConexion; $this->ListaCampos = array (); $this->ListaValores = array (); } function MontaSQL () { $top = count ( $this->ListaCampos ) - 1; $this->SqlQuery = ""; $this->SqlQuery = "insert into " . $this->Tabla . " ("; for ( $i = 0 ; $i <= $top ; $i++ ) { $this->SqlQuery = $this->SqlQuery . " " . $this->ListaCampos [ $i ] . " "; if ( $i <> $top ) $this->SqlQuery = $this->SqlQuery . " , "; } $this->SqlQuery = $this->SqlQuery . ") values ( "; for ( $i = 0 ; $i <= $top ; $i++ ) { $this->SqlQuery = $this->SqlQuery . "'" . $this->ListaValores [ $i ] . "'"; if ( $i <> $top ) $this->SqlQuery = $this->SqlQuery . " , "; } $this->SqlQuery = $this->SqlQuery . " )"; } function Exec () { $this->MontaSQL(); if ( !mysql_query ( $this->SqlQuery , $this->IdConexion ) ) { $this->Errno = mysql_errno(); $this->Error = "Ha habído un error en el Insert SQL" . "
" . "Query: " . $this->SqlQuery . "
" . "Error: " . mysql_error(); if ( $this->MuestraError ) echo $this->Error; } } } // fin de la clase SQLInsert /* Autor: Miguel Matas Oliva */ /* Fecha Creación: 04/12/2004 */ /* Fecha Última Modificación: 04/12/2004 */ class SQLSelect { var $IdConexion; var $Errno; var $Error; var $IdConsulta; var $Seleccionados; var $Condiciones; // where's, having's var $Otros; // order by por ejemplo function SQLSelect ( $Tabla , $Seleccionados , $Condiciones, $Otros , $IdConexion , $MuestraError = 0 ) { $this->Tabla = $Tabla; $this->MuestraError = $MuestraError; $this->SqlQuery = ""; $this->Errno = 0; $this->Error = ""; $this->IdConexion = $IdConexion; $this->IdConsulta = 0; $this->Seleccionados = $Seleccionados; $this->Otros = $Otros; $this->Condiciones = $Condiciones; } function MontaSQL () { $this->SqlQuery = ""; $this->SqlQuery = "select " . $this->Seleccionados . " from " . $this->Tabla . " "; $this->SqlQuery = $this->SqlQuery . $this->Condiciones . " "; $this->SqlQuery = $this->SqlQuery . $this->Otros; } function Exec () { $this->MontaSQL(); $this->IdConsulta = mysql_query ( $this->SqlQuery , $this->IdConexion ); if ( !$this->IdConsulta ) { $this->Errno = mysql_errno(); $this->Error = "Ha habído un error en la Consulta SQL" . "
" . "Consulta: " . $this->SqlQuery . "
" . "Error: " . mysql_error(); if ( $this->MuestraError ) echo $this->Error; return 0; } /* Si hemos tenido éxito en la consulta devuelve el identificador de la conexión, sino devuelve 0 */ return $this->IdConsulta; } function Recordset () { if ( $this->IdConsulta ) return mysql_fetch_array ( $this->IdConsulta ); } function Object () { if ( $this->IdConsulta ) return mysql_fetch_object ( $this->IdConsulta ); } function Rowset () { if ( $this->IdConsulta ) return mysql_fetch_row ( $this->IdConsulta ); } /* Autor: Miguel Matas Oliva */ /* Fecha Creación: 05/11/2004 */ /* Fecha Última Modificación: 16/11/2004 */ /* Descripción: Elimina de memoria una consulta realizada con éxito */ /* Respuesta: Si la consulta no se ha realizado con éxito y la intentamos liberar, php muestra un warning por pantalla */ /* Parámetros: No utiliza parámetros, usa las propiedades de la clase */ function DesmontarSelect () { mysql_free_result ( $this->IdConsulta ); } /* Autor: Miguel Matas Oliva */ /* Fecha Creación: 05/11/2004 */ /* Fecha Última Modificación: 16/11/2004 */ /* Descripción: Cuenta el número de registros de una consulta sql */ /* Respuesta: Si la consulta no se había realizado con éxito, php devuelve un warning */ /* Parámetros: No utiliza parámetros, usa las propiedades de la clase */ function ContarRegistros() { return mysql_num_rows ( $this->IdConsulta ); } /* Autor: Miguel Matas Oliva */ /* Fecha Creación: 05/11/2004 */ /* Fecha Última Modificación: 16/11/2004 */ /* Descripción: Devuelve el número de campos de una consulta */ /* Respuesta: Si la consulta no se había realizado con éxito, php devuelve un warning */ /* Parámetros: No utiliza parámetros, usa las propiedades de la clase */ function ContarCampos() { return mysql_num_fields ( $this->IdConsulta ); } /* Devuelve el nombre de un campo de una consulta */ function NombreCampo ( $numcampo ) { return mysql_field_name ( $this->IdConsulta, $numcampo ); } /* muestra la consulta */ function MostrarConsulta ( $align = "left" , $width = "100%" , $border = "0" ) { // primero mostramos la cabecera y luego los registros echo ""; echo ""; for ( $i = 0 ; $i < $this->ContarCampos() ; $i++ ) { echo ""; } echo ""; while ( $row = mysql_fetch_row ( $this->IdConsulta ) ) { echo ""; for ($i = 0; $i < $this->ContarCampos(); $i++) { echo ""; } echo ""; } echo "
" . $this->NombreCampo($i) . "
".$row[$i]."
"; } } // fin de la clase SQLSelect