print for each student all the subject related to him

Do not post support requests, bug reports or feature requests. Discuss phpBB here. Non-phpBB related discussion goes in General Discussion!
Suggested Hosts
Post Reply
simpleman
Registered User
Posts: 1
Joined: Wed May 05, 2021 1:06 am

print for each student all the subject related to him

Post by simpleman »

hi guys :

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 ???
User avatar
HiFiKabin
Community Team Member
Community Team Member
Posts: 6671
Joined: Wed May 14, 2014 9:10 am
Location: Swearing at the PC, UK
Name: James
Contact:

Re: print for each student all the subject related to him

Post by HiFiKabin »

Hello

This is the support board specifically for phpBB, not for general php or other coding questions so you will get more useful help from somewhere like stackoverflow
Post Reply

Return to “phpBB Discussion”