Call to a Member Function query() on null in /var/www/html/mvc/models/Anuncios.php:9

Asked

Viewed 882 times

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 {}

  • hello Helder, please also post the code of the homeController.

  • What are you trying to do? When the error occurs? Use some library/framework?

  • 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

2 answers

1


puts a global $db; and see if it catches

  • is already global in config.php file

  • but I think you need to add a global inside the keys no? I at least do so and no problem at all

0

Hello Helder try to change the name of the variable that receives the query, maybe it is conflicting because you are declaring the same variable($sql) twice.

$res = $this->db->query($sql);
if ($res->rowCount() > 0) {
        $sql = $res->fetch();
        return $sql['quantidade'];
    } else {
        return 0;
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.