Mysql PHP循环遍历多个数据库表
假设我有一个如下所示的数据库结构:Mysql PHP循环遍历多个数据库表,mysql,loops,foreign-key-relationship,Mysql,Loops,Foreign Key Relationship,假设我有一个如下所示的数据库结构: 学生 +--------------+--------------------+-------------------+ | student_id | student_firstname | student_lastname | +--------------+--------------------+-------------------+ | 1 | John | Doe
学生
+--------------+--------------------+-------------------+
| student_id | student_firstname | student_lastname |
+--------------+--------------------+-------------------+
| 1 | John | Doe |
+--------------+--------------------+-------------------+
| 2 | Lisa | Doe |
+--------------+--------------------+-------------------+
| 3 | Derp | Doe |
+--------------+--------------------+-------------------+
缺席
+--------------+--------------------+-------------------+---------------+
| absence_id | absence_student_id | absence_date | absence_note |
+--------------+--------------------+-------------------+---------------+
| 1 | 2 | 2012-06-10 | A note... |
+--------------+--------------------+-------------------+---------------+
| 2 | 3 | 2012-06-30 | Another note. |
+--------------+--------------------+-------------------+---------------+
以及列student.student\u id
和缺勤。缺勤\u student\u id
在PHP中,我希望循环浏览上述表格,并获得学生
中的行的完整输出,如果缺席
中有与该学生匹配的记录,则对该行执行一些操作,然后继续循环。我希望的HTML输出如下:
<table>
<thead>
<tr>
<td>Name:</td>
<td>Absence date:</td>
<td>Absence note:</td>
</tr>
</thead>
<tr>
<td>John Doe</td>
<td></td>
<td></td>
</tr>
<tr>
<td>Lisa Doe</td>
<td>2012-06-10</td>
<td>A note...</td>
</tr>
<tr>
<td>Derp Doe</td>
<td>2012-06-30</td>
<td>Another note.</td>
</tr>
</table>
<?php
while ($row = mysql_fetch_array($students_sql_query)) {
echo "<tr><td>".$row['student_firstname']."</td></tr>";
}
?>
姓名:
缺勤日期:
缺勤通知:
无名氏
丽莎·多伊
2012-06-10
一张便条。。。
德普能源公司
2012-06-30
另一张纸条。
我过去经常这样循环:
<table>
<thead>
<tr>
<td>Name:</td>
<td>Absence date:</td>
<td>Absence note:</td>
</tr>
</thead>
<tr>
<td>John Doe</td>
<td></td>
<td></td>
</tr>
<tr>
<td>Lisa Doe</td>
<td>2012-06-10</td>
<td>A note...</td>
</tr>
<tr>
<td>Derp Doe</td>
<td>2012-06-30</td>
<td>Another note.</td>
</tr>
</table>
<?php
while ($row = mysql_fetch_array($students_sql_query)) {
echo "<tr><td>".$row['student_firstname']."</td></tr>";
}
?>
是否可以向此添加某种
if
语句?我需要两个不同的MySQL
query和某种foreach
循环吗?我对网络编程比较陌生。。。提前感谢,很抱歉发了这么长的帖子。您需要在查询中加入表,然后从查询中获取结果
SELECT Students.student_firstname
, Students.student_lastname
, Absence.absence_date
, Absence.absence_note
FROM Students
LEFT JOIN Absence ON Students.id = Absence.absence_student_id
然后,您应该能够像下面这样循环:
<?php
while ($row = mysql_fetch_array($students_sql_query))
{
?>
<tr>
<td>$row['student_firstname']</td>
<td>$row['student_lastname']</td>
<td>$row['absence_date']</td>
<td>$row['absence_note']</td>
</tr>
<?
}
?>
$row['student_firstname']
$row['student\u lastname']
$row[“缺勤日期”]
$row[“缺勤通知”]
左连接将告诉数据库把所有学生都拉进来,如果他们缺课,它也将填充这些学生。如果该学生没有缺勤,则将从数据库返回null。当它显示在循环的表行中时,如果该学生的缺勤表中没有任何内容,则它应显示为空列
唯一的缺点是,如果一个学生有多次缺勤,它将在显示表中复制行。在这种情况下,您必须在代码或显示器中进行一些预处理,以处理该情况。使用pdo将如下所示:
$sql = $dbh->prepare('
SELECT student_firstname, student_lastname, absense_date, absense_note
FROM students s inner join absense a
ON s.student_id=a.absense_student_id
');
$sql->execute();
$result = $sql->fetchAll(); <----- fetch All the data
return $result;
foreach($result as $row)
{
echo '<tr>';
echo '<td>'.$row['student_firstname'].' '.$row['student_lastname'].'</td>';
echo '<td>'.$row['absense_date'].'</td>';
echo '<td>'.$row['absense_note'].'</td>';
echo '</tr>';
}
$sql=$dbh->prepare('
选择学生名、学生名、缺席日期、缺席备注
从学生的内心加入缺席
关于s.student\u id=a.absense\u student\u id
');
$sql->execute();
$result=$sql->fetchAll();什么意思?你想退出循环还是改变它的处理方式?很酷,非常感谢!我会将此标记为一个答案,但我必须等待几分钟,然后再这样做。。。