0
can help me with this mistake?
Fatal error: Uncaught Error: Call to a Member Function query() on null in /var/www/html/mvc/models/Anuncios.php:9 Stack trace: #0 /var/www/html/mvc/controllers/homeController.php(10): Announcements->getQuantidade() #1 /var/www/html/mvc/core/Core.php(37): homeController->index() #2 /var/www/html/mvc/index.php(20): Core->run() #3 {main} thrown in /var/www/html/mvc/models/Anuncios.php on line 9
Announcements.php
<?php
class Anuncios extends model
{
public function getQuantidade()
{
$sql = "SELECT COUNT(*) as quantidade FROM epi";
$sql = $this->db->query($sql);
if ($sql->rowCount() > 0) {
$sql = $sql->fetch();
return $sql['quantidade'];
} else {
return 0;
}
}
}
config.php
<?php
require_once 'environment.php';
$config = array();
if (ENVIRONMENT == "development") {
define("BASE_URL", "http://localhost/mvc/");
$config['host'] = 'localhost';
$config['dbname'] = 'self_epi';
$config['dbuser'] = 'root';
$config['dbpass'] = '32051217';
} else {
//define("BASE_URL", "http://www.site.com.br");
$config['host'] = 'localhost';
$config['dbname'] = 'selfepi';
$config['dbuser'] = 'root';
$config['dbpass'] = 'Admin';
}
global $db;
try {
$db = new PDO("mysql:dbname=".$config['dbname'].";host=".$config['host'], $config['dbuser'], $config['dbpass']);
echo 'conectou';
} catch(PDOException $e) {
echo "Erro: ".$e->getMessage();
}
php model.
<?php
class model
{
protected $db;
public funciton __construct()
{
global $db;
$this->db = $db;
}
}
controller
<?php
class homeController extends controller
{
public function index()
{
$anuncios = new Anuncios();
$usuarios = new Usuarios();
$dados = array(
'quantidade' => $anuncios->getQuantidade(),
'nome' => $usuarios->getNome(),
'idade' => $usuarios->getIdade()
);
$this->loadTemplate('home',$dados);
}
}
Hello Helder, avoid putting code images, put your own code in text on the question and format using the button {}
– Ricardo Pontual
hello Helder, please also post the code of the homeController.
– Valdir Silva
What are you trying to do? When the error occurs? Use some library/framework?
– rbz
I am doing a project in mvc, when I try to return the database data gives this error in the query() function, I believe that the file is not able to use the $db connection variable
– Helder Macedo