It is unusual to have a class called mysqli the getInstance() method return the Fatal error: Uncaught Error: Call to Undefined

Asked

Viewed 77 times

0

Hello I’m making a system to search list the data in the database for the month only it give the following error

Fatal error: Uncaught Error: Call to Undefined method mysqli::getInstance()

and in my view it’s all right.

this is the query .

<?php

require('config.php');

class Usuario {
    private $connection = null;

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

    public function carregaSetores($data) {
        try {
            $Query = "SELECT 
                    s.nome,
                    s.snome,
                    s.telefone,
                    s.refeicao,
                    s.bebida,
                    s.data,
                    s.hora,
                    s.npessoa,
                    s.nmesa,
                FROM pedido 
            ";

            $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();
        }
    }
}
    ?>

and this is the code that generates the report.

<?php

                require('config.php');
                require('usuario.php');


                $data    = $_POST["data"];                 
                $Usuario = new Usuario($dbConnetion);
                $listaDados = $Usuario->carregaSetores($data);



                   if(!empty($listaDados)){

                   foreach($listaDados as  $value){
              //echo "<pre>"; print_r($value); exit;
                 echo "<tr>";
                 echo "<td><center>". $value["nome"] ."</center></td>";
                 echo "<td><center>". $value["snome"] ."</center></td>";
                 echo "<td><center>". $value["telefone"] ."</center></td>";
                 echo "<td>". $value["refeicao"] ."</td>";
                 echo "<td>". $value["bebida"] ."</td>";
                 echo "<td><center>". $value["data"] ."</center></td>";
                 echo "<td><center>". $value["hora"] ."</center></td>";
                 echo "<td>". $value["npessoa"] ."</td>";
                 echo "<td>". $value["nmesa"] ."</td>";
                 echo "</tr >";

                                   }
    }


     ?>

and what connects to the database

<?php

    // Creating a database connection

    $connection = mysqli_connect("localhost", "root", "", "peixaria");
    if (!$connection) {
        die("Database connection failed: " . mysqli_connect_error());
    }


    // Selecting a database 

    $db_select = mysqli_select_db($connection, "peixaria");
    if (!$db_select) {
        die("Database selection failed: " . mysqli_connect_error());
    }

?>

the error would be on that line

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

  • It’s unusual to have a class called mysqli the method getInstance() return what? can put it in question.

  • I put that as a question .

  • 1

    I don’t know that function mysqli::getInstance, I’m sure it’s not native to php, so it must be a class you picked up somewhere or you invented, anyway you call it mysqli, but the expected Exception is PDOException, as far as I know Mysqli is not a database is a php API (extension) to access the mysql database.

  • @Guilhermenascimento actually is $p_sql = mysql::getInstance()->prepare($Query);

  • 1

    @allanaraujo still yes this does not exist natively in PHP, and therefore without the code there is no way to know in fact what happened.

  • @Guilhermenascimento already put the code . just wanted to generate the report .

  • 2

    Unfortunately I still can’t see where this code comes from mysql::getInstance, what you put was the User class and a foreach, but the code where you declared the function getInstance and the class mysql are not in the question. Do they really exist? If there is no answer to your problem.

  • 1

    By your config.php code, you’re mixing PDO with mysqli, with functions that don’t exist, meaning you’re randomly programming without understanding how they work and reading the documentation, just copying random codes and mixing things that don’t make sense, It’s not supposed to work. This is a constructive criticism, do not go about doing things without reading the doc (http://php.net) and see the examples there and understand them there.

  • http://php.net/manual/en/book.mysqli.php

Show 4 more comments

1 answer

3

Do not mix Mysqli with PDO, use one or the other but not both. As comments and documentation there is no method getInstance() in class mysqli.

The code is more for PDO than Mysqli so to fix the problem the first step is to create a PDO connection. In the file config.php remove the Mysqli instructions and switch to:

$dbConnetion = new PDO('mysql:host=localhost;dbname=peixaria;', 'root', '');

In the User class modify the method carregaSetores() remove the getInstance() and the property connection to send the queries to the bank.

Change:

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

To:

$p_sql = $this->connection->prepare($Query);

Browser other questions tagged

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