Problem in instantiation using pdo / Fatal error: Uncaught Error: Class

1

The following error appears when trying to instantiate and call a particular method:

  

Fatal error: Uncaught Error: Class 'User' not found in   C: \ xampp \ htdocs \ Projects \ reports.php: 126 Stack trace: # 0 {main}   thrown in C: \ xampp \ htdocs \ Projects \ reports.php online 126

The first screen contains the search form and the second one is where the result will be displayed.

The following code is from the screen that displays the result.

<?php
    require 'queryconection.php';

    //Instanciando a classe
    $Usuario new Usuario();
    $listaDados = $Usuario->carregaSetores($setor, $host, $month);

    if (!empty($listaDados)) {
        foreach ($listaDados as  $value) {
            echo "<pre>"; 
            print_r($value);
            exit();

            echo "<tr>";
                echo "<td><center>". $value["setor"] ."</center></td>";
                echo "<td><center>". $value["usuario"] ."</center></td>";
                echo "<td><center>". $value["hd"] ."</center></td>";
                echo "<td>". $value["memoria"] ."</td>";
                echo "<td>". $value["processador"] ."</td>";
                echo "<td><center>". $value["cd"] ."</center></td>";
                echo "<td><center>". $value["placam"] ."</center></td>";
                echo "<td>". $value["host"] ."</td>";
                echo "<td>". $value["monitor"] ."</td>";
                echo "<td><center>". $value["nobreak"] ."</center></td>";
                echo "<td><center>". $value["placar"] ."</center></td>";
                echo "<td>". $value["placav"] ."</td>";
            echo "</tr >";
        }
    }
?>

This is my query, who can solve my error I give the point

<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$servidor = "localhost";
$usuario = "root";
$senha = "";
$dbname = "cadastro";

//Criar a conexao
$link = new mysqli ("localhost", "root", "", "cadastro");
if ($link->connect_errno) {
    echo"Nossas falhas local experiência ..";
    exit();
}

function carregaSetores($setor,$host,$month){               
    try {
        $Query = "SELECT 
                s.setor,
                s.usuario,
                s.hd,
                s.memoria,
                s.cd,
                s.placam,
                s.host,
                s.monitor,
                s.nobreak,
                s.placar,
                s.placav
            FROM setor            
        ";

        $p_sql = mysql::getInstance()->prepare($Query);
        $_retorno = array(); 

        if ($p_sql->execute()) {
            while ($_result = $p_sql->fetch(PDO::FETCH_ASSOC))  {
                $_retorno[] = $_result; 
            }
        }

        return $_retorno;
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}
?>
    
asked by anonymous 07.10.2016 / 17:44

2 answers

0

Apparently there is no user class defined in your code. It would be interesting to separate the second excerpt from your question and create a specific file for the user class that will have the carregaSetores function. See:

File database / connection.php

<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$servidor = "localhost";
$usuario = "root";
$senha = "";
$dbname = "cadastro";

$dbConnetion = new mysqli ("localhost", "root", "", "cadastro");
if ($link->connect_errno) {
    echo"Nossas falhas local experiência ..";
    exit();
}

File classes / user.php

<?php
class Usuario {
    private $connection = null;

    public function __construction($connection) {
        $this->connection = $connection;
    }

    public function carregaSetores($setor, $host, $month) {
        try {
            $Query = "SELECT 
                    s.setor,
                    s.usuario,
                    s.hd,
                    s.memoria,
                    s.cd,
                    s.placam,
                    s.host,
                    s.monitor,
                    s.nobreak,
                    s.placar,
                    s.placav
                FROM setor as s   
            ";

            $p_sql = mysql::getInstance()->prepare($Query);
            $_retorno = array(); 

            if ($p_sql->execute()) {
                while ($_result = $p_sql->fetch(PDO::FETCH_ASSOC))  {
                    $_retorno[] = $_result; 
                }
            }

            return $_retorno;
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

When using, be sure to give require to files. If you want to make your life easier and avoid using it, study about autoloads and namespaces .

To use the code it would look like this:

require 'database/connection.php';
require 'classes/usuario.php';

$Usuario = new Usuario($dbConnetion);
$listaDados = $Usuario->carregaSetores($setor, $host, $month);

Beware of how you use the variable where the connection is written, you can inadvertently overwrite its heat and waste hours trying to figure out what is wrong.

Some remarks

You can make several improvements to your code, one of the main ones is formatting. Use a pattern that the community has been adopting ( Original page and Portuguese , so your code will be much better seen by the market, in addition to being much easier to identify the problems faced during development.

    
28.12.2016 / 01:21
0

Change the fourth line of:

$Usuario new Usuario();

To:

$Usuario = new Usuario();
    
27.12.2016 / 23:13