Fatal error: Call to a member function fetch () on a non-object

0

I am trying to extract the data from a given cabin (it must be a whole number: 1, 2, 3, ... n).

Why do you tell me that you have not defined "images" in the Cabanas.php file? I have defined var_dump ($ data); so you can tell me what you get: (should not you get 4 results, because all the data is identical except the images (having 4 images)? Only shows one?

array(12) { 
 ["idcabana"]=> string(1) "1" 
 [0]=> string(1) "1" 
 ["nombre"]=> string(7) "CABAÑA1" 
 [1]=> string(7) "CABAÑA1" 
 ["capacidad"]=> string(1) "9" 
 [2]=> string(1) "9" 
 ["descripcion"]=> string(57) "HABITACIÓN DE CABAÑA MUY GRANDE CON TODOS LOS ACCESORIOS." 
 [3]=> string(57) "HABITACIÓN DE CABAÑA MUY GRANDE CON TODOS LOS ACCESORIOS." 
 ["precio"]=> string(6) "150.00" 
 [4]=> string(6) "150.00" 
 ["ruta"]=> string(13) "cabana1_1.jpg" 
 [5]=> string(13) "cabana1_1.jpg" 
} 

I have the following methods:

static public function datosCabana($idcabana){
        //Realizamos la consulta.
        $ejecucion = self::Conexion();
        $registro = $ejecucion->query("SELECT c.idcabana, c.nombre, c.capacidad, c.descripcion, c.precio, i.ruta FROM cabanas as c, imagenes as i
                                   WHERE c.idcabana=i.idcabana AND idcabana=$idcabana");
        //Guardamos la fila de la consulta.
        $datos = $registro->fetch();
        //Creamos un objeto de la clase Cabanas con los datos.
        $micabana = new Cabanas($datos);
        //Pasamos el cliente a la funcion mostrarCabana();.
        $micabana->mostrarCabana();
    }

static public function obtenerImagenesCabana($idcabana){
        //Realizamos la consulta.
        $ejecucion = self::Conexion();
        $sql = "SELECT ruta FROM imagenes WHERE idcabana=$idcabana";
        $registro = $ejecucion->query($sql);
        //Creamos una array de imagenes.
        $imagenes = array();
        //Mientras haya datos los guardamos...
        while($datos = $registro->fetch()){
            //Array asociativo: al array $imagenes le pasamos la imagen concreta.
            array_push($imagenes, $datos["ruta"]);
        }
        //Devolvemos el array de imagenes.
        return $imagenes;
    }

Cabañas Class:

<?php

/**
 * Clase Cabañas.
 */
class Cabanas {
    private $idcabana;
    private $nombre;
    private $capacidad;
    private $descripcion;
    private $precio;
    private $imagenes; //Array

    /**
     * Constructor de la clase Cabanas.
     * @param type $row
     */
    public function __construct($row){
        $this->idcabana = $row["idcabana"];
        $this->nombre = $row["nombre"];
        $this->capacidad = $row["capacidad"];
        $this->descripcion = $row["descripcion"];
        $this->precio = $row["precio"];
        $this->imagenes = $row["imagenes"];
    }

    function getIdcabana() {
        return $this->idcabana;
    }

    function getNombre() {
        return $this->nombre;
    }

    function getCapacidad() {
        return $this->capacidad;
    }

    function getDescripcion() {
        return $this->descripcion;
    }

    function getPrecio() {
        return $this->precio;
    }

    function getImagenes() {
        return $this->imagenes;
    }

    function setIdcabana($idcabana) {
        $this->idcabana = $idcabana;
    }

    function setNombre($nombre) {
        $this->nombre = $nombre;
    }

    function setCapacidad($capacidad) {
        $this->capacidad = $capacidad;
    }

    function setDescripcion($descripcion) {
        $this->descripcion = $descripcion;
    }

    function setPrecio($precio) {
        $this->precio = $precio;
    }

    function setImagenes($imagenes) {
        $this->imagenes = $imagenes;
    }


    function mostrarCabana(){
        echo $this->idcabana;
        echo $this->nombre;
        echo $this->capacidad;
        echo $this->descripcion;
        echo $this->precio;
    }

}

    
asked by omaza1990 04.12.2017 в 21:45
source

1 answer

1

If you run:

SELECT c.idcabana, c.nombre, c.capacidad, c.descripcion, c.precio, i.ruta FROM cabanas as c, imagenes as i
WHERE c.idcabana=i.idcabana AND idcabana=1

I should tell you that idcabana is ambiguous. It should be, instead

SELECT c.idcabana, c.nombre, c.capacidad, c.descripcion, c.precio, i.ruta FROM cabanas as c, imagenes as i
WHERE c.idcabana=i.idcabana AND c.idcabana=1
    
answered by 05.12.2017 / 00:24
source