-
<?php
-
-
/*
-
* clase ConsultaMysql
-
* version 1.1
-
* autor Joan|Garnet http://www.joangarnet.com
-
*/
-
-
-
class ConsultaMysql
-
{
-
/////////////////////////
-
// PROPIEDADES PÚLICAS //
-
/////////////////////////
-
-
var $servidor_mysql; // direccion del servidor Mysql, normalmente es "localhost"
-
var $usuario; // usuario del servidor Mysql
-
var $password; // password del servidor Mysql
-
var $bbdd; // nombre de la base de datos
-
var $consulta; // aquí debes meter la consulta Mysql
-
var $separador; /* si vas a usar el mdo result_par_valor_split(),
-
puedes asignar como separar los strings.
-
Por defecto se separan con "#"*/
-
var $testeando; // true o false
-
var $email_admin; /* si "$testeando" es false, entonces pon
-
el email del admin aqui para que reciba
-
notificaciones por email de los errores mysql */
-
var $resXML; // para almacenar el resultado de la query en formato XML
-
-
//////////////////////////
-
// PROPIEDADES PRIVADAS //
-
//////////////////////////
-
-
var $conexion;
-
var $selec_bbdd;
-
var $string_sucio;
-
var $string_limpio;
-
var $resultado;
-
var $par_valor;
-
var $par_valor_split;
-
var $par_valor_indx;
-
var $largo_separador;
-
var $indice;
-
var $valor;
-
var $n_filas;
-
var $n_columnas;
-
var $fila;
-
var $arr_asoc;
-
-
-
/////////////////////////////
-
// CONSTRUCTOR DE LA CLASE //
-
/////////////////////////////
-
-
function ConsultaMysql ()
-
{
-
$this->separador = "#";
-
}
-
-
//////////////////////
-
// MÉTODOS PRIVADOS //
-
//////////////////////
-
-
function err ()
-
{
-
if ($this-> testeando )
-
{
-
echo "<b><font color='red'>ERROR:</b> --> </b>" .
-
mysql_errno ()."</b> - <i>" .mysql_error ()."</i></font>";
-
exit ();
-
}
-
else
-
{
-
echo "<b><font color='red'>Ha habido un error</font></b>";
-
if ($this-> email_admin )
-
{
-
echo ", el administrador ha sido informado por email";
-
mail ($this-> email_admin ,
-
"Error mysql en" .$_SERVER ['PHP_SELF'] ,
-
"Error-> " .mysql_error ().
-
"\n en->" .$_SERVER ['PHP_SELF'].$_SERVER ['QUERY_STRING'].
-
"\n a las-> " .date ('H:i:s - D-d-m-Y'));
-
}
-
exit ();
-
}
-
}
-
-
//////////////////////
-
// MÉTODOS PÚBLICOS //
-
//////////////////////
-
-
function conectar ()
-
{
-
// conecta a la bbdd
-
$this-> conexion = @mysql_connect ($this-> servidor_mysql , $this-> usuario , $this-> password ) or $this-> err ();
-
$this-> selec_bbdd = @mysql_select_db ($this-> bbdd , $this-> conexion ) or $this-> err ();
-
}
-
function escapar_chars ()
-
{
-
// añade contrabarras a las palabras que lleven ' " \
-
$this-> string_limpio = addslashes ($this-> string_sucio );
-
return ($this-> string_limpio );
-
}
-
function desescapar_chars ()
-
{
-
// quita las contrabarras para mostrar el texto original con ' " \
-
$this-> string_limpio = stripslashes ($this-> string_sucio );
-
return ($this-> string_limpio );
-
}
-
function ejecutar_consulta ()
-
{
-
// ejecuta consulta Mysql
-
$this-> resultado = @mysql_query ($this-> consulta , $this-> conexion ) or $this-> err ($this-> resultado ) or $this-> err ();
-
return ($this-> resultado );
-
}
-
function result_num_filas ()
-
{
-
// devuelve el numero total de filas (horizontal) de la consulta
-
$this-> n_filas = mysql_num_rows ($this-> resultado );
-
return $this-> n_filas;
-
}
-
function result_num_columnas ()
-
{
-
// devuelve el numero total de columnas (vertical) de la consulta
-
$this-> n_columnas = mysql_num_fields ($this-> resultado );
-
return $this-> n_columnas;
-
}
-
function result_array_asoc ()
-
{
-
// devuelve un array asociativo de la consulta (por nombres de campo envez de por indice de campo)
-
while ($this-> fila = mysql_fetch_assoc ($this-> resultado ))
-
{
-
$this-> arr_asoc [] = $this-> fila;
-
}
-
return ($this-> arr_asoc );
-
}
-
function result_par_valor_split ()
-
{
-
// devuelve una cadena "var0=valor1,valor2,valor3&var1=valor1,valor2,valor3&var2= etc..." con los resultados de la consulta
-
$this-> result_array_asoc ();
-
$this-> par_valor = array ();
-
$this-> largo_separador = strlen ($this-> separador );
-
for ($k = 0; $k <sizeof ($this-> arr_asoc ); $k ++)
-
{
-
while ($this-> valor = current ($this-> arr_asoc [$k]) and $this-> indice = key (($this-> arr_asoc [$k])))
-
{
-
if ( ! array_key_exists ($this-> indice , $this-> par_valor ))
-
{
-
$this-> par_valor [$this-> indice] = </SPAN>;</FONT></DIV>
<LI style="FONT-WEIGHT: bold; COLOR: #26536a">
<DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"><FONT face=Arial><FONT size=2> <SPAN style="FONT-WEIGHT: bold; COLOR: #006600">}</SPAN></FONT></FONT></DIV>
<LI style="FONT-WEIGHT: normal; COLOR: #3a6a8b; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace">
<DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"><FONT face=Arial size=2> <SPAN style="COLOR: #0000ff">$this</SPAN>-> <SPAN style="COLOR: #006600">par_valor</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #006600">[</SPAN><SPAN style="COLOR: #0000ff">$this</SPAN>-> <SPAN style="COLOR: #006600">indice</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #006600">]</SPAN> .= <SPAN style="COLOR: #0000ff">$this</SPAN>-> <SPAN style="COLOR: #006600">valor</SPAN> .<SPAN style="COLOR: #0000ff">$this</SPAN>-> <SPAN style="COLOR: #006600">separador</SPAN>;</FONT></DIV>
<LI style="FONT-WEIGHT: bold; COLOR: #26536a">
<DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"><FONT size=2><FONT face=Arial> </FONT><SPAN style="COLOR: #000066"><FONT face=Arial>next</FONT></SPAN><FONT face=Arial> <SPAN style="FONT-WEIGHT: bold; COLOR: #006600">(</SPAN><SPAN style="COLOR: #0000ff">$this</SPAN>-> <SPAN style="COLOR: #006600">arr_asoc</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #006600">[</SPAN><SPAN style="COLOR: #0000ff">$k</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #006600">]</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #006600">)</SPAN>;</FONT></FONT></DIV>
<LI style="FONT-WEIGHT: normal; COLOR: #3a6a8b; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace">
<DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"><FONT face=Arial><FONT size=2> <SPAN style="FONT-WEIGHT: bold; COLOR: #006600">}</SPAN></FONT></FONT></DIV>
<LI style="FONT-WEIGHT: bold; COLOR: #26536a">
<DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"><FONT face=Arial><FONT size=2> <SPAN style="FONT-WEIGHT: bold; COLOR: #006600">}</SPAN></FONT></FONT></DIV>
<LI style="FONT-WEIGHT: normal; COLOR: #3a6a8b; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace">
<DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"><FONT face=Arial><FONT size=2> <SPAN style="COLOR: #616100">foreach</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #006600">(</SPAN><SPAN style="COLOR: #0000ff">$this</SPAN>-> <SPAN style="COLOR: #006600">par_valor</SPAN> <SPAN style="COLOR: #616100">as</SPAN> <SPAN style="COLOR: #0000ff">$indice</SPAN> => <SPAN style="COLOR: #0000ff">$valor</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: #006600">)</SPAN></FONT></FONT></DIV>
<LI style="FONT-WEIGHT: bold; COLOR: #26536a">
<DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"><FONT face=Arial><FONT size=2> <SPAN style="FONT-WEIGHT: bold; COLOR: #006600">{</SPAN></FONT></FONT></DIV>
<LI style="FONT-WEIGHT: normal; COLOR: #3a6a8b; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace">
<DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"><FONT face=Arial size=2> <SPAN style="COLOR: #0000ff">$this</SPAN>-> <SPAN style="COLOR: #006600">par_valor_split</SPAN> .= <SPAN style="COLOR: #0000ff">$indice</SPAN> .<SPAN style="COLOR: #ff0000">"="</SPAN> .<SPAN style="COLOR: #0000ff">$valor</SPAN>;</FONT></DIV>
<LI style="FONT-WEIGHT: bold; COLOR: #26536a">
<DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"><FONT size=2><FONT face=Arial> <SPAN style="COLOR: #0000ff">$this</SPAN>-> <SPAN style="COLOR: #006600">par_valor_split</SPAN> = </FONT><SPAN style="COLOR: #000066"><FONT face=Arial>substr</FONT></SPAN><FONT face=Arial> <SPAN style="FONT-WEIGHT: bold; COLOR: #006600">(</SPAN><SPAN style="COLOR: #0000ff">$this</SPAN>-> <SPAN style="COLOR: #006600">par_valor_split</SPAN> , <SPAN style="COLOR: #800000">0</SPAN>, <SPAN style="FONT-WEIGHT: bold; COLOR: #006600">(</SPAN></FONT><SPAN style="COLOR: #000066"><FONT face=Arial>strlen</FONT></SPAN><FONT face=Arial> <SPAN style="FONT-WEIGHT: bold; COLOR: #006600">(</SPAN><SPAN style="COLOR: #0000ff">$this</SPAN>-> <SPAN style="COLOR: #006600">par_valor_split</SPAN> </FONT></FONT></DIV></LI></OL><