0

I am learning OOP and PDO. Iam very dumb and so got stuck with this error. What do you guys think?.

Fatal error: Call to a member function prepare() on a non-object in /var/www/op/DatosLibros.php on line 35

DatosLibros.php

<?php
class DatosLibros
{ 
    private $cadenaConexion;
    private $user;
    private $password;
    private $objetoConexion;

    public function __construct($cadenaConexion,$user,$password)
    {
         $this->cadenaConexion=$cadenaConexion;
         $this->user=$user;
         $this->password=$password;
    }

    public function conectar ()
    {
        try
        {
            $this->objetoConexion= new PDO ($this->cadenaConexion,$this->user,$this->password);
            $this->objetoConexion->setAttribute(PDO::ATTR_EARMODE,PDO::EARMODE_EXCEPTION);
        }
        catch (PDOException $ex)
        {
            echo "Se ha presentado un problema a la hora de conectar con la base de datos";
        }
    }

    public function desconectar ()
    {
        $this->objetoConexion=null;
    }

    public function ejecutar ($strComando)
    {
        try
        {
            $ejecutar = $this->objetoConexion->prepare($strComando);
            $ejecutar->execute();
            $rows= $ejecutar->fetchAll();
        }
        catch (PDOException $ex)
        {  
            throw $ex;
        }
    } 
} 
?>

Error line:

$ejecutar = $this->objetoConexion->prepare($strComando);

This one uses DatosLibros.php and is called NegociosLibros.php. I just added this part to you people see it the way it is. Here it is:

  <?php
  include_once ("DatosLibros.php");

  class capaNegocios
  {
  public $codigo_libro;
  public $nombre_libro;
  public $descripcion_libro;
  public $autor_libro;
  public $categoria_libro;
  public $editorial_libro;
  public $cantidad_libro;
  public $objetoDatos;

  public function __construct($codigo_libro,$nombre_libro,$descripcion_libro,$autor_libro,$categoria_libro,$editorial_libro,$cantidad_libro)
  {
  $this->codigo_libro=$codigo_libro;
  $this->nombre_libro=$nombre_libro;
  $this->descripcion_libro=$descripcion_libro;
  $this->autor_libro=$autor_libro;
  $this->categoria_libro=$categoria_libro;
  $this->editorial_libro=$editorial_libro;
  $this->cantidad_libro=$cantidad_libro;
  $this->objetoDatos=new DatosLibros ('mysql:host=localhost;dbname=b','root','');
  }

   public function insertar()
   {
   try
   {
   $this->objetoDatos->conectar();
   $this->objetoDatos->ejecutar("insert into libros(codigo_libro,nombre_libro,descripcion_libro,categoria_libro,editorial_libro,cantidad_libro) values('$this->codigo_libro','$this->nombre_libro','$this->descripcion_libro','$this->autor_libro','$this->categoria_libro','$this->editorial_libro','$this->cantidad_libro')");
  $this->objetoDatos->desconectar();
   }
   catch (PDOException $ex)
   {
   throw $ex;
    }
   }  
   public function eliminar()

   {
   $this->objetoDatos->conectar();
   $this->objetoDatos->ejecutar("delete from libros where codigo_libro=$this->codigo_libro");
   $this->objetoDatos->desconectar();
   }

   public function modificar()
   {
   $this->objetoDatos->conectar();
   $this->objetoDatos->ejecutar("update libros set cantidad_libro='$this->cantidad_libro' where codigo_libro=$this->codigo_libro)");
   $this->objetoDatos->desconectar();
   }

   public function mostrar()
   {
   $this->objetoDatos->conectar();
   $fila->$this->objetoDatos->ejecutar("select * from libros where codigo=$this->codigo");
   foreach($fila as $filaActual){
   echo "Codigo del Libro: ",$filaActual [codigo_libro],"<br/>Nombre del Libro:",$filaActual [nombre_libro],"<br/>Descripcion:",$filaActual [descripcion_libro],"<br/>Autor:",$filaActual [autor_libro],"<br/>Categoria:",$filaActual [categoria_libro],"<br/>Editorial:",$filaActual [editorial_libro],"<br/>Cantidad de libros:",$filaActual [cantidad_libro];
   echo "</br>";
    }
  $this->objetoDatos->desconectar();
  }

  }
  ?>
2
  • Is conectar() called/are you sure $this->objetoConexion holds the correct object? Commented Oct 29, 2013 at 21:01
  • try var_dump($this->objetoConexion); and give us the output Commented Oct 29, 2013 at 21:02

1 Answer 1

1

You need to call conectar() before ejecutar($strComando)

Sign up to request clarification or add additional context in comments.

3 Comments

Can anyone tell how i should write it? I just don't get it.
@user2928753 can you post your code that uses this class? it looks like $this->objetoConexion is NULL, which is why $this->objetoConexion->prepare($strComando); fails.
Done my friend! Check it out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.