Why doesn't this simple piece of code work?

Discussion in 'PHP' started by karl_murphy, Oct 15, 2008.

  1. #1
    Hello,

    Could anyone please help me with this simple piece of code? It kind of works, but has a small bug in it. Its meant to query a set of students and return matching results. The first student's results print out fine, however second student has their results printed along with the first student, and the third student has theirs printed along with the first and second, and so on.

    The code is as follows:

    
    $sql2 = "SELECT DISTINCT `CS_CL_SubjectCode` FROM `ClassStudent`
    				WHERE `CS_PU_AdNo` = '" . $row['PU_AdNo'] . "'
    				AND `CS_GT` != 'N'";
    		
    		include("../includes/php/connect2.php");
    		
    		$gt_check = mysql_num_rows($rst2);
    		if ($gt_check > 0)
    		{
    			echo "<div style=\"font:Verdana, Arial, Helvetica, sans-serif; padding:2px; font-size:14px; font-weight:bold; text-align:left; background-color:#1D01F2; color:#FFFFFF; width:776px;\">G and T Status: ";
    			while ($row2 = mysql_fetch_assoc($rst2))
    			{
    				$gt_code[] = $row2['CS_CL_SubjectCode'];
    			} 
    
    			foreach($gt_code as $a_gt_code)
    			{
    				echo $a_gt_code . " (";
    				$sql3 = "SELECT `CS_GT` FROM `ClassStudent`
    						WHERE `CS_PU_AdNo` = '" . $row['PU_AdNo'] . "'
    						AND `CS_CL_SubjectCode` = '" . $a_gt_code . "'";
    						
    				include("../includes/php/connect3.php");
    				
    				while ($row3 = mysql_fetch_assoc($rst3))
    				{
    					$gt_status = $row3['CS_GT'];
    				}
    				echo $gt_status . ") "; 
    			}
    			echo "</div>";
    		}	
    
    PHP:
    Regards.
     
    karl_murphy, Oct 15, 2008 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi, don't really understand the queries, but try to help anyway...
    1. In the printed result - is the value of $a_gt_code always correct??
    2. Is it possible that there is more than one record for this condition in $sql3?? - `CS_PU_AdNo` = '" . $row['PU_AdNo'] . "' AND `CS_CL_SubjectCode` = '" . $a_gt_code . "'";
    Because it feels weird to run while command there, if it's just one record, if it's more, you always get the last fetched record (all previous are overwritten).
    Does it make sense??
     
    lp1051, Oct 15, 2008 IP
  3. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Hi, to answer your questions:

    1. How do you mean "is the value of $a_gt_code always correct"?

    2. For the condition in $sql3 there should on be 1 record returned.

    Hope this helps.

    Regards.
     
    karl_murphy, Oct 15, 2008 IP
  4. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #4
    1. means if this line : echo $a_gt_code . " ("; gives correct (expected) values - so the $sql3 query has the right value to use
    2. in that case, try to add temporary debugger of the returned result :
    Regarding to what you say it should echo the $row3 value only once
     
    lp1051, Oct 15, 2008 IP