Instantiation problem using Pdo/Fatal error: Uncaught Error: Class

Asked

Viewed 98 times

1

The Following error appears when trying to instantiate and call a certain method:

Fatal error: Uncaught Error: Class 'Usuario' not found in C: xampp htdocs Reported projects.php:126 Stack trace: #0 {main} thrown in C: xampp htdocs Project reports.php on line 126

The first screen contains the search form and the second, is where the result will be presented.

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 mistake 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();
    }
}
?>
  • Remember to put up the attribution sign (=) along those lines $Usuario new Usuario();. you don’t need to delete the question and ask it again, just make an edit.

  • @rray you could put as a response to ask so much I as others can benefit .

  • That solved the problem?

  • No , still not showing anything on the screen , type gave an echo "<pre>"; print_r($Usuario); Exit; and still nothing appears .

  • If you give a print_r($listaDados) something comes up?

  • No , appears blank screen also .

  • @rray worked out by putting the = in the line you do that , thank you

Show 2 more comments

2 answers

0

Change the fourth line of:

$Usuario new Usuario();

for:

$Usuario = new Usuario();

0


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

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, do not forget to give a require in the archives. If you want to uncomplicate your life and avoid the use of requires, study about autoloads and namespaces.

To use the code would look like this:

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

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

Be careful how you use the variable where the connection is recorded, you can unintentionally overwrite its heat and spend 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 in Português, so your code will be much better seen by the market, besides being much easier to identify the problems faced during development.

  • Hello I did everything that is recommended above only that I tried everything and it appears these errors . Warning: require(Projects/Connection.php): failed to open stream: No such file or directory in C: xampp htdocs Reported projects.php on line 124 Fatal error: require(): Failed Opening required 'Projects/Connection.php' (include_path='C: xampp php PEAR') in C: xampp htdocs Project reports.php on line 124

  • Your script is in the projects folder, so just put require(connection.php);

Browser other questions tagged

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