Touring a POO query

1

I'm creating my own Framework basic and I just need to go through a%%% query and I've been paused in the SELECT and confirm that the query arrives with its% function get_query parameter but when walking the WHILE is where I mark the error I want to do it with the "SELECT * FROM status WHERE id_status=1" function.

A little help please.

//metodo privado para conectarse a bd
private function db_open(){
    $this->conn = new mysqli(self::$db_Host,self::$db_root,self::$db_pass,$this->db_name);
    $this->conn->set_charset(self::$db_charset);
}

private function db_close(){
    $this->conn->close();
}

//establecer un query simple que afecte datos de tipo insert , delete , update
protected function set_query(){
$this->db_open();
//metodo query heredado de mysqli
$this->conn->query($this->query);
$this->db_close();
}

//obtener resultados de una consulta tipo select en un array
protected function get_query(){
    $this->db_open();
    echo $this->query;
    $result = $this->conn->query($this->query);
    var_dump($result);
    while( $this->registro = $result->fetch_array(MYSQLI_ASSOC) );
    var_dump($this->registro);
    //cerrar consulta de result
    $result->close();
    //cerrams conexion con base de datos
    $this->db_close();
    //quita el ultimo elemento del arreglo arraypop
    return ($this->registro);
}
}
    
asked by Carlos Enrique Gil Gil 04.09.2017 в 00:53
source

2 answers

1

From what I see you want to build an array with the results obtained.

One way to do it would be this:

protected function get_query(){
    $this->db_open();
    echo $this->query;
    $result = $this->conn->query($this->query);

    while($row = $result->fetch_array(MYSQLI_ASSOC))
    {
        $rows[] = $row;
    }

    /*Esto es una prueba de impresión en pantalla*/
    /*Lo puedes quitar luego*/
    foreach($rows as $row)
    {
        echo $row['id_status'];
        echo $row['status'];
    }

    //cerrar consulta de result
    $result->close();
    //cerrams conexion con base de datos
    $this->db_close();

    /*Aquí va el array creado dentro del while 
     *Si quieres aplicar arraypop sobre $rows 
     *también puedes hacerlo antes del return
    */

    return $rows;
}
    
answered by 04.09.2017 в 02:28
0

If you notice you are not doing anything in the loop since you are not encompassing anything inside the brackets of it. In fact, you end it with a ; so your while right now is not doing anything. Simply going through the rows.

while( $this->registro = $result->fetch_array(MYSQLI_ASSOC) );
var_dump($this->registro); //Esta fuera del bucle

You should include it inside the brackets {} to be able to see the results:

while($this->registro = $result->fetch_array(MYSQLI_ASSOC)){
    var_dump($this->registro);
}
    
answered by 04.09.2017 в 01:27