我正在使用以下代码来检索存储在数据库中的学生信息。问题是,如果我使用if
下面的代码然后一切正常,除了我只得到一个与该学生相关的教师详细信息,如果我使用while
那么它会一次又一次地显示整个数据直到教那个学生的老师数量。任何团体都得到了解决方案!
<?php
session_start();
$value = $_SESSION['student_roll'];
require('../includes/connect.php');
$query = "SELECT students.firstname, students.lastname, students.roll, students.semester, students.course, teachers.firstname, teachers.teaching FROM students,teachers WHERE students.roll = $value AND students.semester = teachers.semester";
$run = mysqli_query($conn,$query);
while($result = mysqli_fetch_array($run)){
$students_firstname = $result["0"];
$students_lastname = $result["1"];
$students_roll = $result["2"];
$students_semester = $result["3"];
$students_course = $result["4"];
$teacher_name = $result["5"];
$teacher_teaching = $result["6"];
?>
<h2>Name : <?php echo $students_firstname . " " . $students_lastname?></h2>
<h2>Roll : <?php echo $students_roll ?></h2>
<h2>Semester : <?php echo $students_semester ?></h2>
<h2>Course : <?php echo $students_course ?></h2>
<h2>You're enrolled under : <?php echo $teacher_name ?> & studying <?php echo $teacher_teaching ?></h2>
<?php
} ?>
如果我只使用if
我就得到了这个
http://i.stack.imgur.com/nAUGu.png
使用while
我得到了这个输出。
答案 0 :(得分:0)
问题是一个学生有几个老师,所以一个记录不足以容纳所有信息。将查询拆分为两个:
首先,为学生阅读并输出姓名,学期等:
$query = "SELECT firstname, lastname, semester, course FROM students,teachers WHERE students.roll = $value";
然后,查询该学生的所有教师......
$query = "SELECT teachers.firstname, teachers.teaching FROM students,teachers WHERE students.roll = $value AND students.semester = teachers.semester";
...并在while循环中输出信息。