0
My mistake:
Fatal error: Call to Undefined method DB::prepare() in C: xampp htdocs atTheClub Adm classes houses.php on line 36
Archives:
php houses.
class casas extends Crud{ }
protected $table = 'casas';
private $nomeCasa;
private $pessoaContatoCasa;
private $telefoneCasa;
private $emailCasa;
private $enderecoCasa;
public function setNomeCasa($nomeCasa){
$this->nomeCasa= $nomeCasa;
}
public function setPessoaContatoCasa($pessoaContatoCasa){
$this->pessoaContatoCasa = $pessoaContatoCasa;
}
public function setTelefoneCasa($telefoneCasa){
$this->telefoneCasa= $telefoneCasa;
}
public function setEmailCasa($emailCasa){
$this->emailCasa= $emailCasa;
}
public function setEnderecoCasa($enderecoCasa){
$this->enderecoCasa= $enderecoCasa;
}
public function insert(){
$sql = "INSERT INTO $this->table (nomeCasa, pessoaContatoCasa, telefoneCasa, emailCasa, enderecoCasa) VALUES (:nomeCasa, :pessoaContatoCasa, :telefoneCasa, :emailCasa, :enderecoCasa)";
$stmt = DB::prepare($sql);
$stmt->bindParam(':nomeCasa', $this->nomeCasa);
$stmt->bindParam(':pessoaContatoCasa', $this->pessoaContatoCasa);
$stmt->bindParam(':telefoneCasa', $this->telefoneCasa);
$stmt->bindParam(':emailCasa', $this->emailCasa);
$stmt->bindParam(':enderecoCasa', $this->enderecoCasa);
return $stmt->execute();
}
crud.php
require 'db.php';
abstract class Crud extends DB{
protected $table;
abstract public function insert();
abstract public function update($idCasa);
}
I deleted the delete and search functions to decrease the code snippet.
And below the file where is the DB class that PHP says is the error:
db.php
require 'config.php';
class DB extends PDO{
private static $instance;
public static function getInstance(){
if(!isset(self::$instance)){
try {
self::$instance = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS);
self::$instance->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$instance->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
} catch (PDOException $e) {
echo $e->getMessage();
}
};
return self::$instance;
};
public static function prepare($sql){
return self::getInstance()->prepare($sql);
};
};
So, first I had not extended the PDO class to the BD class, I thought that would solve, tested and generated the same error, in case anyone knows what is happening, I really appreciate it! I deleted all the checks, left only what I was supposed to need to enter the data.
For now note that you are using Singleton, and another thing is, when you inherit from the class
PDO
no longer need to use thenew PDO(...)
, you can just useparent::__construct($dsn, $user, $pass, $opcoes)
, and very easily you could useparent::prepare
instead of what you have there. If there is time and still not solved the problem, I analyze your problem better.– Edilson