Ver Mensaje Individual
  #1 (permalink)  
Antiguo 11/09/2013, 10:41
Avatar de j_silk_h
j_silk_h
 
Fecha de Ingreso: julio-2010
Mensajes: 54
Antigüedad: 14 años, 6 meses
Puntos: 6
Pregunta ¿Como hago para recorrer un array de registros devueltos mediante MYSQLI?

Estoy pasando a usar POO y estoy usando mysqli, yo recuerdo que antes hacia algo como esto:

Código PHP:
$sql mysql_query("select * from maquinas");

while(
$array=mysql_fetch_array($sql)){
   echo 
"Registro: ".$array["ip"];

y asi el mostraba TODOS los registros encontrados

Pero como lo hago en POO usando mysqli?
Les dejo las clases y todo lo que llevo hecho por si quieren ver como voy

Archivo: php.php
Aqui tengo los métodos y clases

Código PHP:
<?php
session_start
();
date_default_timezone_set("America/Caracas");

class 
Conf{
    private 
$_user;
    private 
$_password;
    private 
$_host;
    private 
$_database;
    
    static 
$_instance;
    
    private function 
__construct(){
        require(
"config.php");
        
$this->_user=$user;
        
$this->_password=$password;
        
$this->_host=$host;
        
$this->_database=$db;
    }
    
    private function 
__clone(){
    }
    
    public static function 
getInstance(){
        if(!(
self::$_instance instanceof self)){
            
self::$_instance = new self;
        }
        return 
self::$_instance;
    }
    
    public function 
getUserDB(){
        
$var $this->_user;
        return 
$var;
    }
    
    public function 
getHostDB(){
        
$var $this->_host;
        return 
$var;
    }
    
    public function 
getPassDB(){
        
$var $this->_password;
        return 
$var;
    }
    
    public function 
getDB(){
        
$var $this->_database;
        return 
$var;
    }
}

class 
Db{
    private 
$servidor;
    private 
$usuario;
    private 
$password;
    private 
$database;
    private 
$conex;
    private 
$result;
    
    static 
$_instance;
    
    private function 
setConexion(){
        
$conf Conf::getInstance();
        
$this->servidor=$conf->getHostDB();
        
$this->database=$conf->getDB();
        
$this->usuario=$conf->getUserDB();
        
$this->password=$conf->getPassDB();
    }
    
    private function 
__construct(){
        
$this->setConexion();
        try{
            
$this->conex = new mysqli($this->servidor$this->usuario$this->password$this->database);
        }catch(
Exception $e){
            echo 
$e->getMessage();
        }
    }
    
    public function 
ejecutar($sql){
        if(!
$this->result $this->conex->query($sql)){
            
printf("Error ejecutando: %s\n"$this->conex->error);
        }
    }
    
    public function 
getTotalRegistros(){
        return(
$this->result->num_rows);
    }
    
    public function 
getRegistro($campobd){
        if(
$campobd=="*"){
            return(
$this->result->fetch_array(MYSQLI_ASSOC));
        }else{
            
$array $this->result->fetch_array(MYSQLI_ASSOC);
            return(
$array[$campobd]);
        }
    }
    
    public static function 
getInstance(){
        if(!(
self::$_instance instanceof self)){
            
self::$_instance = new self();
        }
        return 
self::$_instance;
    }
    
}

function 
getUbicacionCliente(){

    
$ip=$_SERVER['REMOTE_ADDR'];
    
$bd Db::getInstance();
    
$bd->ejecutar("SELECT * FROM maquinas_ubicaciones where ip = '".$ip."' ");

    if(
$bd->getTotalRegistros()>1){
        echo 
"Direccion IP comprometida";
    }else{
        
header("Location://".$bd->getRegistro("url"));
    }
}

function 
logear($post){
    
extract($post);
    
$bd Db::getInstance();
    
$clave hash("gost",$clave);
    
$bd->ejecutar("SELECT * FROM usuario where clave = '".$clave."' and usuario = '".$usuario."' ");
    
    if(
$bd->getTotalRegistros() == 1){
        
$_SESSION['usuario_id'] = $bd->getRegistro("usuario");
        
header("Location://localhost/saas/dashboard/");
    }
}

function 
agregarEmpleado($post){
    
extract($post);
    
$bd Db::getInstance();

    
$bd->ejecutar("insert into empleado (
                                        nombres,
                                        apellidos,
                                        cedula,
                                        direccion,
                                        telefono,
                                        fecha_ingreso,
                                        condicion,
                                        cargo,
                                        salario_base
                                        ) values (
                                        '"
.$nombres."',
                                        '"
.$apellidos."',
                                        '"
.$cedula."',
                                        '"
.$direccion."',
                                        '"
.$telefono."',
                                        '"
.$fecha_ingreso."',
                                        '"
.$condicion."',
                                        '"
.$cargo."',
                                        '"
.$salario."'
                                        )"
);
}

?>
Archivo: datos.php
Aqui es donde necesito conseguir todos los registros de empleados con el mismo apellido por decir algo


Código PHP:
<?php
extract
($_GET);
include(
"../bin/php.php");

$bd Db::getInstance();

$bd->ejecutar("select * from empleado where apellido = '".$apellido."' limit 1 ");

print_r($bd->getRegistro("*"));

while(
$bd->getRegistro("*")){
    
//aqui debo mostrar los registros encontrados y solo me llega a mostrar un registro
}
?>
__________________
!Si se puede imaginar se puede programar!