Perform cycle while from a MySQLi query in PHP

1

today I have fallen into the following problem, I am making a student management system in which the data is stored in a MySQL database, but when I want to list them all, I can not do it, I am using the function mysqli_fetch_array (), I've already tried with the mysqli_fetch_assoc () function and I can not get the data, could someone help me? I share my code.

conexion.php

<?php

//Iniciar la conexion
$mysqli = new MySQLi('localhost', 'root', 'contrasenia', 'stacatalina');
if ($mysqli->connect_errno){
    echo("Error debido a ".$mysqli->connect_errno.' - '.$mysqli->connect_error.'\n');
}
return($mysqli);
?>

veralumnos.php

<?php require_once('Connections/conexion.php'); ?>
<?php
 //initialize the session
if (!isset($_SESSION)) {
  session_start();
}
if (!isset($_SESSION['logged'])){
    header('Location: errorPrivilegios.php');
}

$consulta = "SELECT nombres, appaterno, apmaterno FROM perfiles WHERE privilegios='Alumno';";
if($buscardatos = $mysqli->query($consulta)){
            $alumnos = $buscardatos->fetch_array(MYSQLI_ASSOC);
            $buscardatos->free();
    }

else {
        printf("Error: %s\n", $mysqli->error);
        $mysqli->close();
        exit;
    }
?>
<!DOCTYPE html>
<div class="container">
<h1 class="text-center">Alumnos</h1>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
  <table width="80%" border="1" align="center" cellspacing="2" class="table table-responsive table-hover table-striped">
    <tbody>
      <tr>
        <th width="20%" align="center" valign="middle" scope="col"><h4 class="text-center">Matricula</h4></th>
        <th width="50%" align="center" valign="middle" scope="col"><h4 class="text-center">Nombre</h4></th>
        <th width="30%" align="center" valign="middle" scope="col"><h4 class="text-center">Acciones</h4></th>
        </tr>
        <?php while ($row_alumnos = mysqli_fetch_array($alumnos)) { ?>
      <tr>
        <th align="center" valign="middle" scope="row">&nbsp;</th>
        <td align="center" valign="middle"><?php echo ($alumnos['nombres'].' '.$alumnos['appaterno'].' '.$alumnos['apmaterno']); ?></td>
        <td align="center" valign="middle"><button class="btn btn-warning"><span class="glyphicon glyphicon-edit"></span></button>&nbsp;<button class="btn btn-info"><span class="glyphicon glyphicon-usd"></span></button>&nbsp;<button class="btn btn-success"><span class="glyphicon glyphicon-ok-circle"></span></button>&nbsp;<button class="btn btn-danger"><span class="glyphicon glyphicon-erase"></span></button></td>
      </tr>
      <?php } ?>
    </tbody>
  </table>
</div>
</div>

The browser throws me the following error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, array given in C:\xampp\htdocs\stacatalina\veralumnos.php on line 102

Could someone please help me? Thanks.

SOLUTION FOR FUTURE REFERENCES

veralumnos.php

<?php require_once('Connections/conexion.php'); ?>
<?php
 //initialize the session
if (!isset($_SESSION)) {
  session_start();
}
if (!isset($_SESSION['logged'])){
    header('Location: errorPrivilegios.php');
}

$sql = "SELECT nombres, appaterno, apmaterno FROM perfiles WHERE 
privilegios='Alumno'";
$result=mysqli_query($mysqli,$sql);
?>
<!DOCTYPE html>
<div class="container">
<h1 class="text-center">Alumnos</h1>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
  <table width="80%" border="1" align="center" cellspacing="2" class="table table-responsive table-hover table-striped">
    <tbody>
      <tr>
        <th width="20%" align="center" valign="middle" scope="col"><h4 class="text-center">Matricula</h4></th>
        <th width="50%" align="center" valign="middle" scope="col"><h4 class="text-center">Nombre</h4></th>
        <th width="30%" align="center" valign="middle" scope="col"><h4 class="text-center">Acciones</h4></th>
        </tr>
        <?php while($ver=mysqli_fetch_row($result)) { ?>
      <tr>
        <th align="center" valign="middle" scope="row">&nbsp;</th>
        <td align="center" valign="middle"><?php echo ($ver[0].' '.$ver[1].' '.$ver[2]); ?></td>
        <td align="center" valign="middle"><button class="btn btn-warning"><span class="glyphicon glyphicon-edit"></span></button>&nbsp;<button class="btn btn-info"><span class="glyphicon glyphicon-usd"></span></button>&nbsp;<button class="btn btn-success"><span class="glyphicon glyphicon-ok-circle"></span></button>&nbsp;<button class="btn btn-danger"><span class="glyphicon glyphicon-erase"></span></button></td>
      </tr>
      <?php } ?>
    </tbody>
  </table>
</div>
</div>
    
asked by alexchvrches 05.10.2017 в 01:48
source

1 answer

2

Mysqli_fetch_row "gets a row of data from the result set and returns it as an enumerated array, where each column is stored in an array index starting with 0 (zero)". There, apply it to your code. Try it and comment.

<?php require_once('Connections/conexion.php'); ?>
  <!DOCTYPE html>
<div class="container">
<h1 class="text-center">Alumnos</h1>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
 <table width="80%" border="1" align="center" cellspacing="2" class="table 
table-responsive table-hover table-striped">
<tbody>
    <tr>
    <th width="20%" align="center" valign="middle" scope="col"><h4 
  class="text-center">Matricula</h4></th>
    <th width="50%" align="center" valign="middle" scope="col"><h4 
  class="text-center">Nombre</h4></th>
    <th width="30%" align="center" valign="middle" scope="col"><h4 
 class="text-center">Acciones</h4></th>
    </tr>

 <?php
 //initialize the session
 if (!isset($_SESSION)) {
 session_start();
 }
  if (!isset($_SESSION['logged'])){
 header('Location: errorPrivilegios.php');
}

$sql = "SELECT nombres, appaterno, apmaterno FROM perfiles WHERE 
privilegios='Alumno'";
$result=mysqli_query($mysqli,$sql);

while($ver=mysqli_fetch_row($result)){ 

?>


  <tr>


   <td><?php echo $ver[0] ?></td>
   <td><?php echo $ver[1] ?></td> 
   <td><?php echo $ver[2] ?></td> 

    <?php } ?>
    <td align="center" valign="middle"><button class="btn btn-warning"><span 
  class="glyphicon glyphicon-edit"></span></button>&nbsp;<button class="btn 
 btn-info"><span class="glyphicon glyphicon-usd"></span></button>&nbsp;
 <button class="btn btn-success"><span class="glyphicon glyphicon-ok-
 circle"></span></button>&nbsp;<button class="btn btn-danger"><span 
  class="glyphicon glyphicon-erase"></span></button></td>
  </tr>

  </tbody>
  </table>
  </div>
  </div>
    
answered by 05.10.2017 / 06:39
source