class mysqli is not found

Asked

Viewed 295 times

0

PHP is giving the following error: Fatal error: Uncaught Error: Class 'loja\model\mysqli' not found in, he is not finding the mysqli class, but I have checked php.ini and it is not commented. I am using the autoload of the Poser, when I take that part of the code that instances the mysqli class works normally. What could this be, below I will put the codes

 namespace loja\model;
class Banco
{
    public function conectar()
    {

        $con = new mysqli("127.0.0.1","root","","ganha_tempo");

        if($con->connect_errno){
            echo "Erro ao se conectar ao banco de dados";
            mysqli_close();
        }
        return $con;
    }
}

Usersistema class

  namespace loja\model\controleUsuariosSistemasModel;
 session_start();
 use loja\model\usuarioGeralModel\Usuarios;
 use loja\model\Banco;

class Usersistema extends Users { private $unit; private $password;

public function setUnidade($unidade)

{ $this->unit = $unit; }

public Function getUnion() { Return $this->unit; }

public Function setSend($password) { $this->password = $password; }

public Function getSend() { Return $this->password; }

public Function logarSystem($cpfUsuario,$senhaUsuario) { $banco = new Banco();

$con = $banco->conectar();

$resultado = $con->query("SELECT nome,senha,cpf,nome_unidade,nome_funcoes FROM usuarios_sistemas,unidade,funcoes WHERE cpf = '$cpfUsuario' AND senha = '$senhaUsuario' AND id_unidade = fk_unidade AND id_funcoes = fk_funcao");

$controle = false;

while ($consulta = $resultado->fetch_object()) 
{
   if($consulta->cpf == $cpfUsuario && $consulta->senha == $senhaUsuario)
   {
    $nomeUsuario = $consulta->nome;
    $_SESSION['usuario'] = $nomeUsuario;
    $_SESSION['cpf'] = $cpfUsuario;
    $_SESSION['unidade'] = $consulta->nome_unidade;
    $_SESSION['funcao'] = $consulta->nome_funcoes;
    $controle = true;
   }else{
    $controle = false;
   } 
}
$con->close();
return $controle;

}

public Function controleLogin($Cpf) { if($Cpf==null || $Cpf=="") { header('LOCATION:.. /.. /.. /index.php'); } }

public Function restricaoAccess($Cpf,$moduloMenu) { $bd = new bank();

$con = $bd->conectaBD();

$resultado = $con->query("SELECT codigo_menu,modulo_menu FROM menu,usuarios_sistemas,acesso_menu WHERE cpf = fk_usuarios_sistemas AND id_menu = fk_menu AND cpf = $cpf");

while($consulta = $resultado->fetch_object()){
      if($consulta->modulo_menu == $moduloMenu){
        echo $consulta->codigo_menu;
      }
}
$con->close();

}

public function criptografaCpf($cpf) { $cripto = base64_encode($cpf); return $cripto; } }

  • 2

    This is a scope problem because PHP is looking for the class loja\model\mysqli, within your namespace. Add a use mysqli at the beginning of your file to fix the scope.

1 answer

1


Use the "use":

namespace loja\model;

use mysqli;

class Banco
{
    public function conectar()
    {

    $con = new mysqli("127.0.0.1","root","","ganha_tempo");

    if($con->connect_errno){
           echo "Erro ao se conectar ao banco de dados";
            mysqli_close();
        }

        return $con;
    }
}

Or the bar "\":

namespace loja\model;

class Banco
{
    public function conectar()
    {

    $con = new \mysqli("127.0.0.1","root","","ganha_tempo");

    if($con->connect_errno){
           echo "Erro ao se conectar ao banco de dados";
            mysqli_close();
        }

        return $con;
    }
}
  • 1

    thank you very much, solved the problem.

Browser other questions tagged

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