PHP mysqli_fetch_array 如何转到下一行? [英] PHP mysqli_fetch_array how to go to next row?
问题描述
我有一个 php 和 mysql 脚本,我想根据在 mysql 表中找到的值创建不同的结果.
I have a php and mysql script, where I want to create different results based on the values found in mysql table.
我将 horas 数组中的 te 值与该行的 hora 变量的值进行比较.如果它同样我做其他事情,它会做其他事情.但是我的疑问是,在 if 中,当条件为真时如何更改到下一行?
I compare te value in the horas array to the value of my hora variable of the row. If it this same I do something else, it does other. However my doubt is, in the if, when the condition is true how do I change to the next row?
$con = db_connect();
$result = mysqli_query($con,"SELECT * FROM projetor WHERE data = '$mydate' AND hora = '$hora'");
$horas=array("a","b","c","d","e","f","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x");
$arrlength=count($horas);
while($row = mysqli_fetch_array($result)){
$cod = $row['cod'];
$data = $row['data'];
$hora = $row['hora'];
$professor = $row['professor'];
$sala = $row['sala'];
for($x=0;$x<$arrlength;$x++)
{
if ($horas[$x] == $hora){
echo "<tr>";
echo "<td>" .$professor . "</td>";
echo "</tr>";
next($row);//how to go to next row??
}
else{
$h=$horas[$x];
$professor ="<a href='add_reserva.php?hora=$h&mydate=$mydate'>Requisitar</a>";
echo "<tr>";
echo "</tr>";
}
}
}
推荐答案
你不需要调用任何 next
方法,这段代码为你做到了:
You don't need to call any next
method, this code does that for you:
while($row = mysqli_fetch_array($result)){
(已编辑)由于您的 while 循环中有一个 for 循环迭代,您可以放置一个 break;
来结束您的内部 for 循环,这将开始您的 while 循环的下一次迭代(这将转到 SQL 结果的下一行)
(Edited)
Since you have a for-loop iteration inside your while-loop, you can put a break;
that will end your inner for-loop, which will start the next iteration of your while-loop (which will go to the next row of your SQL result)
这篇关于PHP mysqli_fetch_array 如何转到下一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!