debz89uk Posted March 25, 2010 Share Posted March 25, 2010 I am trying to join two tables in mysql and then show the results in php. I have managed to join the two tables but it only shows the last entry/row for each. Here is my php code : $sql = "Select ProgrammingFoundations.student_id, ProgrammingFoundations.attendance, ProgrammingFoundationsLab.lab_attendance from ProgrammingFoundations inner join ProgrammingFoundationsLab on ProgrammingFoundations.student_id = ProgrammingFoundationsLab.student_id"; $result = mysql_query($sql) or die(mysql_error());; if($row = mysql_fetch_array($result)) { while($row = mysql_fetch_array($result)) { echo ' lecture attendance : ', $row['attendance']; echo ' lab attendance : ', $row['lab_attendance']; } } else { echo 'not successful'; } this shows all the rows except the first but i dont know why? Quote Link to comment https://forums.phpfreaks.com/topic/196530-joining-tables/ Share on other sites More sharing options...
irkevin Posted March 25, 2010 Share Posted March 25, 2010 What happends if you try a LEFT JOIN? $sql = "Select ProgrammingFoundations.student_id, ProgrammingFoundations.attendance, ProgrammingFoundationsLab.lab_attendance from ProgrammingFoundations LEFT JOIN ProgrammingFoundationsLab on ProgrammingFoundations.student_id = ProgrammingFoundationsLab.student_id"; $result = mysql_query($sql) or die(mysql_error()); if($row = mysql_fetch_array($result)) { while($row = mysql_fetch_array($result)) { echo ' lecture attendance : '. $row['attendance']; echo ' lab attendance : '. $row['lab_attendance']; } } else { echo 'not successful'; } Quote Link to comment https://forums.phpfreaks.com/topic/196530-joining-tables/#findComment-1031840 Share on other sites More sharing options...
shlumph Posted March 25, 2010 Share Posted March 25, 2010 How many matching student id's in both ProgrammingFoundations and ProgrammingFoundationsLab? Quote Link to comment https://forums.phpfreaks.com/topic/196530-joining-tables/#findComment-1031842 Share on other sites More sharing options...
debz89uk Posted March 25, 2010 Author Share Posted March 25, 2010 Left join shows the same result as inner join. ProgrammingFoundations and ProgrammingFoundationsLab have the same number(all matching) of student_ids, the amount varies depending on how many the user enters. Quote Link to comment https://forums.phpfreaks.com/topic/196530-joining-tables/#findComment-1031844 Share on other sites More sharing options...
debz89uk Posted March 25, 2010 Author Share Posted March 25, 2010 Fixed, by changing code to : $sql = "Select ProgrammingFoundations.student_id, ProgrammingFoundations.attendance, ProgrammingFoundationsLab.lab_attendance from ProgrammingFoundations left join ProgrammingFoundationsLab on ProgrammingFoundations.student_id = ProgrammingFoundationsLab.student_id"; $result = mysql_query($sql) or die(mysql_error());; if($row = mysql_fetch_array($result)) { $sql = "Select ProgrammingFoundations.student_id, ProgrammingFoundations.attendance, ProgrammingFoundationsLab.lab_attendance from ProgrammingFoundations left join ProgrammingFoundationsLab on ProgrammingFoundations.student_id = ProgrammingFoundationsLab.student_id"; $result = mysql_query($sql) or die(mysql_error());; while($row = mysql_fetch_array($result)) { echo ' lecture attendance : '. $row['attendance']; echo ' lab attendance : '. $row['lab_attendance']; } } else { echo 'not successful'; } doh'. Quote Link to comment https://forums.phpfreaks.com/topic/196530-joining-tables/#findComment-1031848 Share on other sites More sharing options...
shlumph Posted March 25, 2010 Share Posted March 25, 2010 This seems a bit odd to me Quote Link to comment https://forums.phpfreaks.com/topic/196530-joining-tables/#findComment-1031854 Share on other sites More sharing options...
Mchl Posted March 25, 2010 Share Posted March 25, 2010 Every time you call mysql_fetch_array it removes one row from resultset, co when doing something like if($row = mysql_fetch_array($result)) { while($row = mysql_fetch_array($result)) { echo ' lecture attendance : ', $row['attendance']; echo ' lab attendance : ', $row['lab_attendance']; } } you lose one row when calling mysql_fetch_array witin if condition Quote Link to comment https://forums.phpfreaks.com/topic/196530-joining-tables/#findComment-1031864 Share on other sites More sharing options...
Psycho Posted March 25, 2010 Share Posted March 25, 2010 Every time you call mysql_fetch_array it removes one row from resultset, co when doing something like if($row = mysql_fetch_array($result)) { while($row = mysql_fetch_array($result)) { echo ' lecture attendance : ', $row['attendance']; echo ' lab attendance : ', $row['lab_attendance']; } } you lose one row when calling mysql_fetch_array witin if condition ^^ Exactly! I think you meant the IF condition to test if the query ran successfully, but that is not what it is doing. Give this logic a try: <?php $sql = "Select pf.student_id, pf.attendance, pfl.lab_attendance FOM ProgrammingFoundations pf INNER JOIN ProgrammingFoundationsLab pfl ON pf.student_id = pfl.student_id"; $result = mysql_query($sql) or die(mysql_error());; if (!$result) { //Query failed echo 'not successful'; } else if (mysql_num_rows($result)===0) { //Query ran, but no results returned echo 'no results returned'; } else { ($row = mysql_fetch_array($result)) { echo " lecture attendance : {$row['attendance']}"; echo " lab attendance : {$row['lab_attendance']}"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/196530-joining-tables/#findComment-1031870 Share on other sites More sharing options...
Mchl Posted March 25, 2010 Share Posted March 25, 2010 Since we have die here: $result = mysql_query($sql) or die(mysql_error()); it means that if query fails, then the script will end before it will get to if (!$result) Quote Link to comment https://forums.phpfreaks.com/topic/196530-joining-tables/#findComment-1031878 Share on other sites More sharing options...
Psycho Posted March 25, 2010 Share Posted March 25, 2010 Since we have die here: $result = mysql_query($sql) or die(mysql_error()); it means that if query fails, then the script will end before it will get to if (!$result) Yeah, I missed that. Quote Link to comment https://forums.phpfreaks.com/topic/196530-joining-tables/#findComment-1031885 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.