Joining tables

Discussion in 'PHP' started by debz89uk, Mar 25, 2010.

  1. #1
    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';
    		}
    PHP:
    this shows all the rows except the first but i dont know why?
     
    debz89uk, Mar 25, 2010 IP
  2. mnvlxxx

    mnvlxxx Peon

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Check if the ID of the first row exists in both tables.
     
    mnvlxxx, Mar 25, 2010 IP
  3. debz89uk

    debz89uk Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah, it does.
     
    debz89uk, Mar 25, 2010 IP
  4. debz89uk

    debz89uk Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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';
    		}
    PHP:
    doh'.
     
    debz89uk, Mar 25, 2010 IP
  5. mnvlxxx

    mnvlxxx Peon

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It's because you are using mysql_fetch_array($result)) twice on the same resultset.

    Like this it should look fine.


    
    <?
            $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'];
                }
    ?>
    PHP:
     
    mnvlxxx, Mar 25, 2010 IP
  6. mnvlxxx

    mnvlxxx Peon

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes... you can make the query twice too. lol
     
    mnvlxxx, Mar 25, 2010 IP
  7. blacksheep666

    blacksheep666 Active Member

    Messages:
    68
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #7
    
    <?
            $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 (!mysql_num_rows($result))
    		{
    			echo "No record found";
    			exit ();
    		}
    			
    		while($row = mysql_fetch_array($result))
    		{
    			echo ' lecture attendance : '. $row['attendance'];
    			echo ' lab attendance : '. $row['lab_attendance'];
    		}
    ?>
    
    PHP:
     
    blacksheep666, Mar 25, 2010 IP