Fatal error: Call to a member function query () on null when making a query

1

I'm trying to do a project with php and mysql in MVC.

but I get this error when I want to ask a question.

The code of the connection and the query is the following:

<?php

class dbNBA{
    private $host="localhost";
    private $user="root";
    private $pass="";
    private $db_name="ufree";

    private $conexion;

    private $error=false;
    
    function __contruct(){
        
        $this->conexion= new mysqli($this->localhost, $this->user, $this->pass,$this->db_name);
        if($this->conexion->connect_errno){
            $this->error=true;
            
        }
    }

    function hayError(){
        return $this->error;
    }

    public function consultarUsuarios(){
        if($this->error==false){
            $resultado=$this->conexion->query("SELECT idE, telefono, correo, CONCAT(nombre, ' ', nombre2, ' ', apellido, ' ', apellido2) as nombrecompleto FROM estudiante");
           return $resultado;
        }else{
            return null;
        }
    }

}

The code of the view is as follows

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Registro</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" media="screen" href="css/style.css" />

    <!-- Materia Design Bootstrap-->
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <!-- Bootstrap core CSS -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet">
    <!-- Material Design Bootstrap -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/mdbootstrap/4.5.12/css/mdb.min.css" rel="stylesheet">
</head>
<body>
<?php require 'partials/header.php';?>
<div class="container">
    <div class="row">
    <div class="col-md-8 principal">
    <table class="table">
  <thead>
    <tr>
      <th scope="col">Identificacion</th>
      <th scope="col">Nombre Completo</th>
      <th scope="col">Telefono</th>
      <th scope="col">Correo</th>
      <th scope="col">Editar</th>
      <th scope="col">Borrar</th>
    </tr>
 
    <?php
    include"config/conexion.php";
    $nba=new dbNBA();

    //recuperar los datos de usuario
    $resultado=$nba->consultarUsuarios();


  while($fila = $resultado->fetch_assoc()){
    ?>
    </thead>
  <tbody>
    <tr>
      <th scope="row"><?php echo $fila["id"];?></th>
      <td><?php echo $fila["nombrecompleto"];?></td>
      <td><?php echo $fila["telefono"];?></td>
      <td><?php echo $fila["correo"];?></td>
      <td><a href="consulta.php?editar=<?php echo $fila["id"];?>"><button type="button" class="btn btn-success">Editar</button></a></td>
      <td><a href="consulta.php?borrar=<?php echo $fila["id"];?>"><button type="button" class="btn btn-danger">Borrar</button></a></td>
    </tr>
  </tbody>
  <?php
    }?>
</table>

<?php 
    if(isset($_GET['editar'])){
        include("editar.php");

    }

?>

<?php
    $con->set_charset("utf8");
	if(isset($_GET['borrar'])){

        
	
    echo $borrar_id = $_GET['borrar'];

    $consultaEstudiante = "SELECT idPorE FROM portafolio WHERE idEP=''$borrar_id'";

    $borrarListado= "DELETE FROM listado WHERE idPorEL='$consultaEstudiante'";
	
    $borrarPortafolio= "DELETE FROM portafolio WHERE idEP='$borrar_id'";
    
    $borrarEstudiante= "DELETE FROM estudiante WHERE idE='$borrar_id'";

    $ejecutar1 = mysqli_query($con,$consultaEstudiante);
    $ejecutar2 = mysqli_query($con,$borrarListado); 
    $ejecutar3 = mysqli_query($con,$borrarPortafolio);  
    $ejecutar4 = mysqli_query($con,$borrarPortafolio);
	$ejecutar5 = mysqli_query($con,$borrarEstudiante); 
		
		if($ejecutar5){
		
		echo "<script>alert('El usuario ha sido borrado!')</script>";
		echo "<script>window.open('consulta.php','_self')</script>";
		}
	
	}
	
	?>
    </div>
    </div>
</div>
   <!--script bootstrap 4-->
<script src="js/main.js"></script>
<script type="text/javascript" src="js/registro.js"></script>
<!-- JQuery -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Bootstrap tooltips -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.4/umd/popper.min.js"></script>
<!-- Bootstrap core JavaScript -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.min.js"></script>
<!-- MDB core JavaScript -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mdbootstrap/4.5.12/js/mdb.min.js"></script>

</body>

</html>
    
asked by Camilo Rodriguez 24.10.2018 в 05:19
source

1 answer

0

Your constructor is misspelled is __construct no __contruct() (missing the "s") so I assume that it never runs when initializing the instance of the class and therefore the property $conexion is never created by what you call it gives you an error that this method does not exist.

    
answered by 24.10.2018 в 07:14