So what have you got to work? Have you been able to add the messages? (Do the tables contain anything?) How do you decide which ones to display? Just show all of them?
$sql and then close the connection, but you apparently never actually run the query. So
$result should be empty and your code would be breaking. NogDog has already suggested that your copy-pasting is leaving something out. I recommend that when you post PHP code you do so between
[/code] tags so that it doesn't get misformatted.
I'm going to go ahead and infer that you're trying to support a conversation between the teacher and the student. For that, you'll need to rethink your data model. As it stands, the teacher is only allowed to say one and only one thing to any student, and each student can say one and only one thing to any teacher.
The model I'm going to put here could be improved further, but should be simple enough to see what is going on.
There is one table for conversations.
conversationID | timestamp | from | to | content
1 | 2019-05-12 12:17:20 | Ben | Robinson | What kind of car was it?
1 | 2019-05-12 12:17:29 | Robinson | Ben | What?
1 | 2019-05-12 12:17:33 | Ben | Robinson | Do you remember the make of the car?
conversationID: an integer; this is the same for every message in a single conversation. Different conversations have different conversationIDs.
timestamp: a date/time; exactly when this message was sent. This of course is so that they can be retrieved in the correct order
from/to: strings; the names of the conversationalists. This bit could really do with normalising out, but for now I'm just going to leave it here
content: another string. The text of the message.
To get all the information of a single conversation for display, the query would be
Select timestamp, from, to, content From conversations Where conversationID=42 Order by timestamp (assuming you want conversation #42 of course).
The reason for including
timestamp in the selected columns is so that you can include it in the display (similar to the way this forum adds things like "5 hours ago" to each post. If you don't need that you don't need it in the select columns, but you will still need to use it in the ordering clause. Otherwise you could get them in any old order.
Since you know who it is from and who it is to, you can use that to label who said what. Maybe use the information to style their lines so that, say, Ben's lines are on the left and Robinson's lines are on the right. Note that there is no insistence that the conversationalists have to take strict turns.
As I said, the design is not fully developed. Most significant is how it repeatedly names the conversationalists every time either of them says anything. That sort of thing would really be done using a two tables. (So the
conversations table would have a single record for each entire conversation, with an ID, the teacher's name, and the student's name (or teacher/student IDs for clarity), and then the actual content table would be as it is here, except that the
to columns would be replaced by a single bit to indicate whether the message was from the teacher to the student or vice versa.