PHP While循环仅获取第一行结果

时间:2016-09-25 07:31:06

标签: php mysql while-loop

我从数据表中的db获取数据,但我的while循环只获取第一行结果。我可以猜到我错了,但不知道在哪里。在下面找到详细信息

表:

用户:

user_id | user_role_id | user_name | fname | lname | profile
:-----: | :----------: | :-------: | :---: | :---: | :-----:
  1     |    2         |  schin    |  sam  |  chin |  t1.png
  2     |    2         |  mlouis   |  mark |  louis|  t2.png

教师:

 t_id   |   classes    | subjects  | pri_classes | primary_subjects | email
:-----: | :----------: | :-------: | :---------: | :--------------: | :-----:
  1     |  eight,nine  |  math,phy |    nine     |       maths      |  1@gmail.com
  2     |    two       |  science  |    two      |       science    |  2@gmail.com

Php查询:

<?php 

$sql3 = "select * from user where user_role_id = 2";
$result3 = $dbh->query($sql3);
$row3 = mysqli_fetch_assoc($result3);

$user_id = $row3['user_id'];

$sql4 = "select * from teachers where t_id = '$user_id'";
$result4 = $dbh->query($sql4);

?>

表头:

<table id="datatable-table" class="table table-striped table-hover">
 <thead>
  <tr>
  <th>S.no</th>
   <th class="no-sort hidden-sm-down">Image</th>
   <th >Name</th>
   <th >User Name</th>
   <th >Classes Handled</th>
   <th >Subjects Handled</th>
   <th >Primary Class</th>
   <th >Primary Subjects</th>
   <th >Email</th>
   </tr>
   </thead>
   <tbody>

while循环:

 <?php
 while(($row4 = mysqli_fetch_assoc($result4))&&($row3)){
 ?>
 <tr align="center">
 <td>
 <?php
 $i = 1;
 echo $i;
 $i++;
 ?>
</td>
<td>
<span >
<img src="../img/<?php echo $row3['profile'];?>" style="width:40px; height:40px;">
</span>
</td>
<td>
<span class="fw-semi-bold">
<?php echo $row3['fname'].' '.$row3['lname']; ?>
</span>
</td>
<td>
<span class="fw-semi-bold">
<?php echo $row3['user_name']; ?>
</span>
</td>
<td><span class="fw-semi-bold">
<?php 
$classes=explode(',', $row4['classes']);
$prefix = '';
foreach($classes as $cout)
{
echo $prefix . '' . wordsToNumber($cout);
$prefix = ', ';
}
?>
</span>
</td>
<td><span class="fw-semi-bold">
<?php 
$subjects=explode(',', $row4['subjects']);
$prefix = '';
foreach($subjects as $sout) {
echo $prefix . '' . str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($sout))));
$prefix = ', ';
}
?>    
</span>
</td>
 <td>
<span class="fw-semi-bold">
<?php 
echo wordsToNumber($row4['pri_classes']);
?> 
</span></td>
<td><span class="fw-semi-bold">
<?php 
echo str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($row4['primary_subjects']))));
?>   
</span></td>
<td><span class="fw-semi-bold"><?php echo wordwrap($row4['email'],10, "<br>\n"); ?></span></td>
</tr>
<?php
}
?>  
</tbody>
</table>

对于 wordsToNumber ,我使用this函数

1 个答案:

答案 0 :(得分:1)

您的第一个查询仅检索单个用户。你需要嵌套循环。

while ($row3 = mysqli_fetch_assoc($result3)) {
    $user_id = $row3['user_id'];

    $result4 = $dbh->query("select * from teachers where t_id = '$user_id'");

    while($row4 = mysqli_fetch_assoc($result4)){
        // display etc
    }
}

您可能需要考虑MySQL中的一些连接以避免这么多查询http://dev.mysql.com/doc/refman/5.7/en/join.html