Fatal error: Uncaught Error: Call to a member function prepare() on null in
-
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.
-
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.