mysql - PHP: For loop that repeats the first item instead of looping through all items? -
i have mysql query requests list of items.
i fetch them , want display every item loop, but shows me first item repeated, instead of every item.
why?
<?php $conectar = mysqli_connect(host, user, pass, database); $query = " select cursoid, nombrecurso, estadocurso cursos estadocurso='abierto'"; $buscarcurso = mysqli_query($conectar,$query); $curso=mysqli_fetch_assoc($buscarcurso); $totalrows = mysqli_num_rows($buscarcurso); //there 3 rows of results echo $totalrows; ($i=0; $i < $totalrows; $i++) { echo '<br>'; echo $curso['nombrecurso']; echo '<br>'; } ?>
the intended result is:
curso 1
curso 2
curso 3
and instead
curso 1
curso 1
curso 1
your loop should fetching result set on every iteration. standard way (as in many examples given in php documentation) in while
condition:
$totalrows = mysqli_num_rows($buscarcurso); //there 3 rows of results echo $totalrows; while ($curso=mysqli_fetch_assoc($buscarcurso)) { echo '<br>'; echo $curso['nombrecurso']; echo '<br>'; }
Comments
Post a Comment