hope , you re doing well especially in this period of the pandemic covid-19 !! i'm writhing this topic because i'm struggling to solve an sql and php code that is simple , here are the details :
1) i have a db with 2 tables :
first one is "student"
id | name | class
===========================
1 | mike | "AAA"
2 | jack | "BBB"
3 | dylan | "CCC"
second is "subject"
id | student_id | description
===================================================
1 | 1 | "matematics"
2 | 1 | "physic"
3 | 2 | "litterature"
4 | 4 | "art"
2) the "student_id" is a foreign key , related to the id of the table "student"
3) here's my code
Code: Select all
<?php
$username = "root";
$password = "root";
$database = "doc-pic";
$mysqli = new mysqli("localhost", $username, $password, $database);
$query = "SELECT * ,
from
student , subject
INNER JOIN student d ON student.id = subject.student_id
where id = student_id
ORDER BY student_id";
echo "<b> <center>subjects related to each student</center> </b> <br> <br>";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
$field1name = $row["doc_id"];
$field2name = $row["description"];
print '<b>'.$field1name.$field2name.'</b><br />';
}
/*freeresultset*/
$result->free();
}
?>
4) my question is ,i want to print for each student all the subject related to him example
| student | subjects
===================================================
| mike | matematics , physic
| jack | litterature
| dylan | art
you can deduce from the example that the student mike has 2 subjects related to him wich are: "matematics , physic" , jack has only 1 : "litterature" ------ could someone help me to resolve that exercice please ???