Error Message, handling functions with POO and PHP .. Fatal error: Call to a member function fetch_all () on a non-object in

2
class OperacionesAlumno{
private $server = 'localhost';
private $usuario = 'root';
private $pass = '';
private $bd = 'dbvasco';

public function Conectar(){
     $conexion = mysqli_connect('localhost','root','','intranet');
     mysqli_set_charset($conexion, 'utf8');  //permite que las tild
     return $conexion;
}

public function muestraGrupos(){
   $conexion = $this->Conectar();
   $sql = mysqli_query($conexion, "SELECT * 'grupos'");
   return $sql->fetch_all();  // LINEA DE ERROR
}
    
asked by Armando Arellano 29.09.2016 в 21:42
source

1 answer

1

Your SQL sentence has a syntax error, and mysqli_query in those cases does not return a object, but FALSE .

I suggest you perform two actions:

a) Change the construction to evaluate the value returned in an IF, it could be as simple as this:

if ($resultado = $mysqli->query($miSentenciaSQL)) {
  printf("La selección devolvió %d filas.\n", $resultado->num_rows);
  $resultado->close();
} else {
  /* no utizes die en la vida real, acá maneja el error o informa al usuario. */
  die "$misqli devolvió FALSE";
}

b) fix your SQL statement (you need the clause from ), I guess what you really want is this:

SELECT * from 'grupos'

Putting together both, and making other small changes, something like this might look like:

public function muestraGrupos(){
   $conexion = $this->Conectar();
   if ($resultado = mysqli_query($conexion, "SELECT * from 'grupos'")) {
     $filas = $resultado->fetch_all();
     $resultado->close();
     return $filas;
   } else {
     return FALSE;
   {
}
    
answered by 29.09.2016 / 22:12
source