I am unable to retrieve values from different tables using inner join, The output always doesn't retrieve anything.. Also, there is no error message and I'm unable to determine what went wrong in my code.. Can someone please help? I would appreciate it very much. Here is a part of my code..
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("student_records",$con);
if(isset($_COOKIE['username']))
{
if($_COOKIE['username']!='admin')
{
$tbl6=mysql_query("SELECT A.LName, A.FName, A.MName, B.YearLevel
FROM student as A INNER JOIN stud_course as B ON A.StudNo=B.StudNo INNER JOIN course as C ON B.CourseCode=C.CourseCode INNER JOIN religion as D ON A.ReligionID=D.ReligionID
WHERE A.StudNo = '".mysql_real_escape_string($_COOKIE['username'])."' ");
while($row=mysql_fetch_array($tbl6))
{
echo "<td>".$row['LName']."</td><td>".$row['FName']."</td><td>".$row['MName']."</td><td>".$row['Course']."</td><td>".$row['YearLevel']."</td><td>".$row['EAdd']."</td><td>".$row['ReligionName']."</td></tr>";
}
}
}
?>
Inner Joins require that data exist in both the tables included in the join. In your case, you have four tables, and any one of those tables could be missing relevant data:
SELECT A.LName, A.FName, A.MName, B.YearLevel, C.Course
FROM student as A
INNER JOIN stud_course as B ON
A.StudNo=B.StudNo
INNER JOIN course as C ON
B.CourseCode=C.CourseCode
INNER JOIN religion as D ON A.ReligionID=D.ReligionID
WHERE A.StudNo = '".mysql_real_escape_string($_COOKIE['username'])."' "
student
table with a StudNo
equal to the username cookie, you will get no data.stud_course
table, you will get no data.CourseCode
in stud_course
does not exist in the course
table, you will get no data.ReligionID
is null or has a religion that is not in the religion
table, you will get no data.I would suggest breaking the SQL into smaller pieces (or possibly switching each INNER JOIN
to LEFT OUTER JOIN
) to debug and discover where the problem is.
Note, you have to make sure to add the Course
and Eadd
(not sure what table that's from) fields to your select list if you are going to try to use it.