How to move to PHP 7 this class and function?

0

I have been migrating from PHP 5 to 7 the code of my project, since mysql does not run on the server but mysqli, but I have a class and function that I have not managed to make it work since with the previous version of PHP if it worked, the code is as follows.

class Consultar_Grado{
    private $consulta;
    private $fetch;

    function __construct($codigo){
        $this->consulta = mysql_query("SELECT * FROM grado WHERE id=$codigo");
        $this->fetch = mysql_fetch_array($this->consulta);
    }

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

Since changing the statements to mysqli or trying other methods always sends errors, I hope you can help me, thanks.

    
asked by Juan Felipe 24.06.2018 в 21:17
source

1 answer

0

Try the following code:

class Consultar_Grado{
private $consulta;
private $fetch;

function __construct($codigo){
    $this->consulta = mysqli_query("SELECT * FROM grado WHERE id='".$codigo."'");
    $this->fetch = mysqli_fetch_array($this->consulta);
}

function consultar($query){
    return $this->mysqli_stmt_fetch[$query];
}
}

I have not tested it but in theory this should work

    
answered by 24.06.2018 в 22:25