Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. Fatal error: Uncaught Error: Call to a member function prepare() on null in

Fatal error: Uncaught Error: Call to a member function prepare() on null in

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
databasehelpphpmysqldata-structures
2 Posts 2 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nicolas Veloso
    wrote on last edited by
    #1

    Hello, I get this error when updating or editing data in a "name" field, I leave this error in more detail: Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\xampp\htdocs\pdo\conectar.php:46 Stack trace: #0 C:\xampp\htdocs\pdo\editar.php(20): Datos->setDatos('update candidat...') #1 {main} thrown in C:\xampp\htdocs\pdo\conectar.php on line 46

    conectar.php:

    conn = new PDO("mysql:dbname=sistema\_votos;host=localhost", "root", "");
    } catch(PDOException $error){
    die("Error: ".$error);
    }
    return $this->conn;
    }
    public function setNames(){
    return $this->conn->query("SET NAMES 'utf8'");
    }
    }
    class Datos extends Conectar{
    private $bd;
    public function \_\_construct(){
    $this->bd = self::conectar();
    self::setNames();
    }
    public function getDatos($sql){
    $datos = $this->bd->prepare($sql);
    if (!$datos) {
    echo "\\nPDO::errorInfo():\\n";
    print\_r($this->bd->errorInfo());
    die();
    }
    $datos->execute();
    $this->bd=null;
    return $datos->fetchAll();
    }
    public function getDato($sql){
    $datos = $this->bd->prepare($sql);
    $datos->execute();
    $this->bd=null;
    return $datos->fetch();
    }
    public function setDatos($sql){
    $datos = $this->bd->prepare($sql);
    $datos->execute();
    //return $this->bd->lastInsertId();
    }
    }

    editar.php

    conectar.php:

    conn = new PDO("mysql:dbname=sistema\_votos;host=localhost", "root", "");
    } catch(PDOException $error){
    die("Error: ".$error);
    }
    return $this->conn;
    }
    public function setNames(){
    return $this->conn->query("SET NAMES 'utf8'");
    }
    }
    class Datos extends Conectar{
    private $bd;
    public function \_\_construct(){
    $this->bd = self::conectar();
    self::setNames();
    }
    public function getDatos($sql){
    $datos = $this->bd->prepare($sql);
    if (!$datos) {
    echo "\\nPDO::errorInfo():\\n";
    print\_r($this->bd->errorInfo());
    die();
    }
    $datos->execute();
    $this->bd=null;
    return $datos->fetchAll();
    }
    public function getDato($sql){
    $datos = $this->bd->prepare($sql);
    $datos->execute();
    $this->bd=null;
    return $datos->fetch();
    }
    public function setDatos($sql){
    $datos = $this->bd->prepare($sql);
    $datos->execute();
    //return $this->bd->lastInsertId();
    }
    }

    I'm desperate, because I can't find the solution, add that I've tested the connection and with respect to that everything works fine, so it's not a connection problem.

    L 1 Reply Last reply
    0
    • N Nicolas Veloso

      Hello, I get this error when updating or editing data in a "name" field, I leave this error in more detail: Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\xampp\htdocs\pdo\conectar.php:46 Stack trace: #0 C:\xampp\htdocs\pdo\editar.php(20): Datos->setDatos('update candidat...') #1 {main} thrown in C:\xampp\htdocs\pdo\conectar.php on line 46

      conectar.php:

      conn = new PDO("mysql:dbname=sistema\_votos;host=localhost", "root", "");
      } catch(PDOException $error){
      die("Error: ".$error);
      }
      return $this->conn;
      }
      public function setNames(){
      return $this->conn->query("SET NAMES 'utf8'");
      }
      }
      class Datos extends Conectar{
      private $bd;
      public function \_\_construct(){
      $this->bd = self::conectar();
      self::setNames();
      }
      public function getDatos($sql){
      $datos = $this->bd->prepare($sql);
      if (!$datos) {
      echo "\\nPDO::errorInfo():\\n";
      print\_r($this->bd->errorInfo());
      die();
      }
      $datos->execute();
      $this->bd=null;
      return $datos->fetchAll();
      }
      public function getDato($sql){
      $datos = $this->bd->prepare($sql);
      $datos->execute();
      $this->bd=null;
      return $datos->fetch();
      }
      public function setDatos($sql){
      $datos = $this->bd->prepare($sql);
      $datos->execute();
      //return $this->bd->lastInsertId();
      }
      }

      editar.php

      conectar.php:

      conn = new PDO("mysql:dbname=sistema\_votos;host=localhost", "root", "");
      } catch(PDOException $error){
      die("Error: ".$error);
      }
      return $this->conn;
      }
      public function setNames(){
      return $this->conn->query("SET NAMES 'utf8'");
      }
      }
      class Datos extends Conectar{
      private $bd;
      public function \_\_construct(){
      $this->bd = self::conectar();
      self::setNames();
      }
      public function getDatos($sql){
      $datos = $this->bd->prepare($sql);
      if (!$datos) {
      echo "\\nPDO::errorInfo():\\n";
      print\_r($this->bd->errorInfo());
      die();
      }
      $datos->execute();
      $this->bd=null;
      return $datos->fetchAll();
      }
      public function getDato($sql){
      $datos = $this->bd->prepare($sql);
      $datos->execute();
      $this->bd=null;
      return $datos->fetch();
      }
      public function setDatos($sql){
      $datos = $this->bd->prepare($sql);
      $datos->execute();
      //return $this->bd->lastInsertId();
      }
      }

      I'm desperate, because I can't find the solution, add that I've tested the connection and with respect to that everything works fine, so it's not a connection problem.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The message (as far as I can tell) refers to the following:

      public function getDatos($sql){
      $datos = $this->bd->prepare($sql);

      And it is telling you that the variable bd has not been initialised.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups