It is unusual to have a class called mysqli the getInstance () method returns the Fatal error: Uncaught Error: Call to undefined [closed]

0

Hello, I'm doing a system to fetch the data in the database for the month only it gives the following error

  

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

It's all right in my eyes.

This is a 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 in this line

  

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

    
asked by anonymous 04.01.2017 / 21:16

1 answer

3

Do not mix MySQLi with PDO, use one or the other but not both. As per the 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 config.php file, remove the MySQLi statements and change to:

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

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

Change:

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

To:

$p_sql = $this->connection->prepare($Query);
    
05.01.2017 / 12:59