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"> </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> <button class="btn btn-info"><span class="glyphicon glyphicon-usd"></span></button> <button class="btn btn-success"><span class="glyphicon glyphicon-ok-circle"></span></button> <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"> </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> <button class="btn btn-info"><span class="glyphicon glyphicon-usd"></span></button> <button class="btn btn-success"><span class="glyphicon glyphicon-ok-circle"></span></button> <button class="btn btn-danger"><span class="glyphicon glyphicon-erase"></span></button></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>