gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:16:18 24 4
gpt4 key购买 nike

我正在从数据表中的数据库获取数据,但我的 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功能

最佳答案

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

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

关于PHP While 循环仅获取第一行结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39684455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com