Ver Mensaje Individual
  #1 (permalink)  
Antiguo 26/02/2011, 04:50
Marrselo
 
Fecha de Ingreso: diciembre-2009
Mensajes: 56
Antigüedad: 15 años, 3 meses
Puntos: 0
Funcion sin funcionar :S ?

Saludos cordiales, esto es una duda algo muy raro que si bien tengo soluciones alternas quisiera aclarar esta incognita,

Me explico, tengo 2 clases, 1 que conecta a la DB llamada Conexion.php y otra que maneja las consultas llamada ManejadorDB.php

class conexion {

private $host = "localhost";

private $database = "";
private $username = "";
private $password = "";
private $link;
private $result;
public $sql;


function __construct($database=""){

if (!empty($database)){ $this->database = $database; }

$this->link = mysql_connect($this->host,$this->username,$this->password);

mysql_select_db($this->database,$this->link);

return $this->link;



}


function EjecutarSQL($sql){

if (!empty($sql)){

$this->sql = $sql;

$this->result = mysql_query($sql,$this->link) or die(mysql_error()."error en EJECUTAR<br /><br />".$sql);

return $this->result;

}else{

return false;

}

}
function fetch($result=""){

if (empty($result)){ $result = $this->result; }

return mysql_fetch_array($result);

}
}


la segunda es:
A esta le paso como atributo el nombre de cualquier tabla de mi db, la clase contiene varios metodos(insertar, actualizar, borrar , consultar etc )obviare los demas y les mostrare solo 2 de ellos que se llaman consulta() y arrayConsulta(). detallando:

class ManejadorDB{

private $_db;
protected $_tabla = "";


function __construct($tabla){
$this->_db = new conexion();
$this->_tabla = $tabla;
}

public function consulta()
{
$this->query; //publico
$this->cad; //publico
$sql = $this->_db->EjecutarSQL(" $this->query from $this->_tabla where 1 $this->cad ");

//return " $this->query from $this->_tabla where 1 $this->cad ";
return $sql;

$this->_db->free_result($sql) or die(mysql_error());
}


// REVISAR...... :(
public function arrayConsulta(){
$this->query;
$this->cad;
$sql = $this->_db->EjecutarSQL(" $this->query from $this->_tabla where 1 $this->cad ");
if ($this->consulta()){
while($fila = $this->_db->fetch($sql)) {
$todo[] = $fila;
}
}

}

}

.....probando la clase con cualquier tabla digamos tabla persona (que ndría 2 campos: nombre y edad )



$Obj = new ManejadorDB("sar_vdr");


$Obj->query= "select nombre, edad"; //solo seleccion el nombre
$ObjSar->cad = " and edad >'15' "; //

$sql = $ObjSar->consulta(); // esto funciona bien, me trae la consulta y puedo maniobrar,


Lo que no entiendo porque este metodo me devuelve vacio y es que :
$fila = $ObjSar->arrayConsulta();


public function arrayConsulta(){
$this->query;
$this->cad;
$sql = $this->_db->EjecutarSQL(" $this->query from $this->_tabla where 1 $this->cad ");
if ($this->consulta()){
while($fila = $this->_db->fetch($sql)) {
$todo[] = $fila;
}
}
return $todo;

}


El metodo arrayConsulta() funciona bien , LA DUDA ESTA EN ESTA LINEA:

// he puesto $sql que viene del metodo consulta()

while($fila = $this->_db->fetch($sql)) { //AQUI DUDA

PORQUE NO ES CORRECTO PONER ESTO??

while($fila = $this->_db->fetch($this->consulta()) { //???

esa es mi duda porque cuando hago me sale error como este:
Fatal error: Allowed memory size of 25165824 bytes exhausted (tried to allocate 264 bytes) in I:\AppServ\www\idip\idip\controles\clases\Conexion .php on line 125