Help with PHP MYSQL error

0

I have the following problem when loading data on the web:

  

Notice: Undefined variable: connection in C: \ xampp2 \ htdocs \ Modules \ class_buscar.php on line 41

     

Warning: mysqli_query () expects parameter 1 to be mysqli, null given in C: \ xampp2 \ htdocs \ Modules \ class_buscar.php on line 41

     

Warning: mysqli_fetch_array () expects parameter 1 to be mysqli_result, null given in C: \ xampp2 \ htdocs \ Modules \ class_buscar.php on line 42

Where in the file class_buscar.php I have this that are lines 41 and 42: **

class Consultar_Profesor{
    private $consulta;
    private $fetch;

    function __construct($codigo){
        $this->consulta=mysqli_query($conexion,"SELECT * FROM profesor WHERE doc='$codigo'");
        $this->fetch = mysqli_fetch_array($this->consulta);
    }

    function consultar($campo){
        return $this->fetch[$campo];
    }
}

and the file of conexion I have it like this:

$conexion = mysqli_connect("localhost","root","");
mysqli_select_db($conexion,"basedatos1");

Help please thanks

    
asked by Francisco 09.05.2017 в 14:48
source

2 answers

0

As commented the error is by the connection variable, I had a lot and after trying several ways I chose to do something similar to how you have, on the one hand a connection file where the data is the same as yours .

The classes the same as you have also, the only difference is that when you instantiate a class I pass the connection variable.

For example, in the constructor method of the class you have shown, it would be like this:

function __construct($codigo,$conexion){
        $this->consulta=mysqli_query($conexion,"SELECT * FROM profesor WHERE doc='$codigo'");
        $this->fetch = mysqli_fetch_array($this->consulta);
    }

In this way you only have to modify the argument of the constructor on what you already had. Try to see.

    
answered by 09.05.2017 / 19:41
source
0

It does not work for you because the connection variables are not included in your class, or at least that's what you see in the code you've put in.

Since you are doing it with classes you could have a file conexion.php that contains a class to save the connection data and then invoke in your class Consultar_Profesor .

Connection Class

Class Conexion {

    protected $conexion_db;
    private $host = 'nombre_servidor';
    private $bd = 'nombre_base_de_datos';
    private $user = 'nombre_usuario_base_de_datos';
    private $pass = 'contraseña_usuario_base_de_datos';

    public function __construct(){

        try{

            $this->conexion_db = new mysqli($host, $user, $pass, $bd);

            }catch(Exception $e){

                echo "Error en la conexión: " . $e->getLine();

            }
    }

}

Class Consulta Teacher

Then in your file Consultar_Profesor you would call connection in this way:

if(!class_exists('Conexion')) {

    include("includes/conexion.php");

}  

class Consultar_Profesor extends Conexion{

        function __construct($codigo){

            parent::__construct();

            $sql = "SELECT * FROM profesor WHERE doc='$codigo'";
            $query = $this->conexion_db->query($sql);
            $resultado = $query->fetch_all(MYSQLI_ASSOC);
            return $resultado;

        }

        function consultar($campo){

            return $this->fetch[$campo];

        }

}

To be able to use it you have to create a new object of the class Consultar_Profesor in the file where you are executing this code.

Example

$consultaprofesor = new Consultar_Profesor($codigo)

And then you use that object as before to generate the query.

    
answered by 09.05.2017 в 15:34