-1
I am trying to list a database table however I am not succeeding and getting the error "Fatal error: Uncaught Error: Call to a Member Function prepare() on null in C: xampp htdocs service_desk_pi classes Model.php:28 Stack trace: #0 C: xampp htdocs service_desk_pi equipamento.php(7): Model->listar() #1 {main} thrown in C:xampp htdocs service_desk_pi classes Model.php on line 28"
Model.php
<?php
require 'Conexao.php';
class Model
{
protected $tabela;
protected $class;
protected $db;
public function __construct()
{
$conexao = new Conexao();
$this->db = $conexao->conectar();
}
public function inserir($values)
{
$sql = "INSERT INTO {$this->tabela} VALUES ($values)";
$stmt = $this->db->prepare($sql);
$stmt->execute();
return $this->db->lastInsertId();
}
public function listar()
{
$sql = "SELECT * FROM {$this->tabela}";
$stmt = $this->db->prepare($sql);
$stmt->setFetchMode(PDO::FETCH_CLASS, $this->class);
$stmt->execute();
return $stmt->fetchAll();
}
public function get($id)
{
$sql = "SELECT * FROM {$this->tabela} WHERE id = {$id}";
$stmt = $this->db->prepare($sql);
$stmt->setFetchMode(PDO::FETCH_CLASS, $this->class);
$stmt->execute();
return $stmt->fetch();
}
public function alterar($id, $values)
{
$sql = "UPDATE {$this->tabela} SET {$values} WHERE id = {$id}";
$stmt = $this->db->prepare($sql);
$stmt->execute();
}
public function deletar($id)
{
$sql = "DELETE FROM {$this->tabela} WHERE id = {$id}";
$stmt = $this->db->prepare($sql);
$stmt->execute();
}
}
Connexion.php
<?php
class Conexao
{
private $host;
private $user;
private $password;
private $conn;
public function __construct() {
$this->host = 'mysql:host=localhost;dbname=teste';
$this->user = 'root';
$this->password = '';
}
public function conectar() {
try {
$conn = new PDO(
$this->host,
$this->user,
$this->password
);
return $conn;
} catch (PDOException $e) {
echo 'Erro na conexao. Erro reportado: ' . $e->getMessage();
exit;
}
}
}
Equipmentdao.php
<?php
require 'Model.php';
class EquipamentoDAO extends Model
{
public function __construct() {
$this->tabela = 'equipamento';
$this->class = 'Equipamento';
}
}
index php.
<?php include 'layout/header.php';
?>
<?php
require 'classes/Equipamento.php';
require 'classes/EquipamentoDAO.php';
$equipamentoDAO = new EquipamentoDAO();
$equipamentos = $equipamentoDAO->listar();
?>
<div class="row" style="margin-top:40px">
<div class="col-10">
<h2>Gerenciar equipamentos</h2>
</div>
<div class="col-2">
<a href="form_equipamento.php" class="btn btn-success">Nova</a>
</div>
</div>
<div class="row">
<table class="table table-hover table-bordered table-striped">
<thead>
<tr>
<th>#ID</th>
<th>id_chamado</th>
<th>Marca</th>
<th>Modelo</th>
<th>Categoria</th>
</tr>
</thead>
<tbody>
<?php foreach($equipamentos as $equipamento){ ?>
<tr>
<td><?= $equipamento->getId() ?></td>
<td><?= $equipamento->getMarca() ?></td>
<td>
<a href="form_equipamento.php?id=<?= $equipamento->getId() ?>">Editar</a> |
<a href="controle_equipamento.php?acao=deletar&id=<?= $equipamento->getId() ?>" onclick="return confirm('Deseja realmente excluir?')">Excluir</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php include 'layout/footer.php'; ?>
Solved, thank you very much.
– Guilherme TJ