I have the following code to perform a dynamic query of JOINED tables but for some reason, whenever I try to display the row, it only displays up the first 9 rows then stops.
$query = "SELECT `results`, `Success`, `Failure`, `Counter`, `Grades`, `Classes`,
`Special_Id`, `SpecialCondition` FROM `Courses` INNER JOIN `Students` ON `id` =
`Students_Id`";
And this is how I’m fetching the rows and displaying them:
<?php
foreach($results as $row){
echo $row['grades'].'<br />';
}
I tested the JOIN SQL syntax in phpMyAdmin and while it returns as a positive task, I notice that it only shows the original table and not the joined table after being joined.
Any suggestions on where i’m going wrong here? I’m only showing this portion of the code because everything works fine, and when I remove the JOIN syntax it works beautifully, so I’m lead to believe the joining is causing the issue but I can’t see what I’m doing wrong.
EDITED TO SHOW SQL SCHEMA:
Sure, Hopefully the following helps.
Table: Courses
id (PK)| results | Success | Failure | Counter | Grades | Classes |
1 Posted 1 0 2 B+ 2
2 Pending 0 0 1 NA 1
3 Posted 0 1 3 F 1
4
5
6
7
8
9
10+
Table: Students
Students_Id (PK) | FirstName | LastName |
1 Chris Test
2 Jen Test
3
4
5
6
7
8
9
10+
To keep things easy to read, I removed the information in the other 10+ rows, but there is data populating them.
Also, I noticed in the OP, I’m selecting “Special_Id and SpecialConditions”. That’s actually wrong. I’m not selecting those fields anymore.
When joining tables, does every column have to have data in-order to successfully INNER JOIN or can there be empty rows as well?
Your query shows that you are joining where the
idfrom theCoursestable matches theStudents_IdfromStudents. However, the schema makes it look as thoughidis not actually a student identifier but rather just a record index in that table. It seems to me that you need to have an additional column inCoursesfor the key to theStudentstable.Note that you only told the query to select columns that were included in the
Coursestable. You probably also wanted to selectStudents.FirstNameandStudents.LastName(or something similar).