クラス内の複数の生徒のテスト結果を表示しようとしています。これを行うには、関数find_student_by_year($ year)を使用して、学生IDを含むすべての生徒のリストを取得しています。 IDは、特定の学生IDの結果を照会するためにSQLステートメントで使用されます。私のコードはエラーを投げていませんが、テーブルに実際にデータがあってもデータは表示されません。残念ながら、私の研究は何も似ていないので、私の投稿にリンクは追加されていません。コードはPHP関数から返された値をクエリとテーブルのデータ表示に使用するにはどうすればよいですか?
function find_student_by_year($year) {
global $connection;
$query = "select * from students where entry_year = {$year} order by s_fname asc";
$found_students = mysqli_query($connection, $query);
confirm_query($found_students);
return $found_students;
}
PHPやHTML(混合)コード
<tbody>
<?php
$staff_id = 27;
$subject_id = 2;
//Actually getting this value from $_GET but am hard coding it here
$year = 4;
if (isset($year)) {
$student_year = find_student_by_year($year);
} else {
$student_year = null;
}
if (isset($student_year)) {
while ($year_group = mysqli_fetch_assoc($student_year)) {
?>
<tr>
<td><input name="student_id[]" value="<?php echo htmlentities($year_group["student_id"]) ?>" readonly></td>
<td><?php echo $year_group["s_fname"] . " " . $year_group["s_mname"] . " " . $year_group["s_lname"]; ?></td>
<?php
$result = mysqli_query($connection, "select * from results where student_id = {$year_group['student_id']} and subject_id = {$subject_id}");
while($result_record = mysqli_fetch_assoc($result)) {
?>
<td style="width: 15%"><input type="text" name="test1[]" class="form-control" style="text-align: center"
value="<?php if ($result_record['test1'] !== ' ') { echo $result_record['test1']; } else {echo ' ';} ?>">
</td>
<td style="width: 15%"><input type="text" name="test2[]" class="form-control" style="text-align: center"
value="<?php if ($result_record['test2'] !== ' ') { echo $result_record['test2']; } else {echo ' ';} ?>">
</td>
<td style="width: 15%"><input type="text" name="test3[]" class="form-control" style="text-align: center"
value="<?php if ($result_record['test3'] !== ' ') { echo $result_record['test3']; } else {echo ' ';} ?>">
</td>
<td style="width: 15%"><input type="text" class="form-control" style="text-align: center"
value="<?php echo htmlentities($result_record['final']); ?>" readonly>
</td>
</tr>
<?php } } } ?>
</tbody>
参照データベースのテーブルやディスプレイスクリーンショット機能
以下 で働いています参照してください。
'$結果= mysqli_query($接続、「year_group STUDENT_ID = {$結果SELECT * FROM以下のコード抽出を見ます["student_id"]}とsubject_id = {$ subject_id} "); ' - " $ year_gro '$ year_group [student_id ']' –
また、ネストしたwhileループの中括弧はありません: 'while($ result_record = mysqli_fetch_assoc($ result))' ** {* student_id " * –
そのようなループでクエリを実行するのは一般的に貧弱な設計です。 2つのテーブルを結合する1つのクエリを使用します。 – Barmar