如何使用mysql_fetch_assoc处理php中的记录?

enter image description here

i am trying to retrieve subject names, there are two types of subjects optional and main. Each student contains optional and main subjects. by using those subject ids i can retrieve the name from subject table. But in this result only one optional subject name is appearing, is there any problem in my code? pls help me

  $subject_names = array();
for($i=0;$i<count($student_num_data);$i++)
{
    $optional_id_list = mysql_query("SELECT optional_subject_id FROM ms_student WHERE student_id = ".$student_num_data[$i]['student_id']);
    while($row = mysql_fetch_assoc($optional_id_list))
    {
        foreach ($row as $key) 
        {
            $optional_subject = mysql_query("SELECT subject_name FROM ms_subject WHERE subject_id = ".$key['optional_subject_id']); 
            $optional_subject_name = array();
            while($row1 = mysql_fetch_assoc($optional_subject))
            {
                $optional_subject_name[] = $row1;  
            }
        }
    }
    $subject_id_list = mysql_query("SELECT subject_ids FROM subject_config WHERE stream_index =".$stu_stream);  
    while($row = mysql_fetch_assoc($subject_id_list))
    {
        foreach ($row as $key) 
        {
            $main_subject = mysql_query("SELECT subject_name FROM ms_subject WHERE subject_id = ".$key['subject_ids']); 
            $main_subject_name = array();
            while($row3 = mysql_fetch_assoc($main_subject))
            {
                $main_subject_name[] = $row3;  
            }
        }
    } 
    $subject_names = $main_subject_name[0]['subject_name'].','.$optional_subject_name[0]['subject_name'];

    $small_subject_name=trim($subject_names);//remove whitespace at end of string         
    $small_subject_name = str_replace(" ", "-", $small_subject_name); // Replaces all spaces with hyphens.     
    $small_subject_name = preg_replace('/[^A-Za-z0-9\-]/', '', $small_subject_name); // Removes special chars.     
    $small_subject_name =  preg_replace('/-+/', '-', $small_subject_name); // Removes multiple hyphens.            
    $small_subject_name = str_replace("-", " ", $small_subject_name); // Replaces all hyphens with spaces.            
    $small_subject_name=strtolower(str_replace(" ","_",$small_subject_name));  

    if($stu_class == 11 || $stu_class == 12)
    {  
        $subject_statement = $subject_statement."IF(".$small_subject_name."='ab','ab',round(".$small_subject_name.",2)) AS ".$small_subject_name.",";   
        $sum_subject_statement = $sum_subject_statement.'IFNULL(`'.$small_subject_name.'`,0) + ';           
        /* start by 1022 02-09-2014 */
        $subject_pass_statement .= $small_subject_name." >= 33 AND ";       
        $subject_pass_statement = substr($subject_pass_statement,0,strlen($subject_pass_statement)-5);
        $update_pass_statement = "UPDATE ".$table_name." SET class_rank = 'P' WHERE 1=1 ".$track_div_stmnt.$id_stmnt." AND student_id = ".$student_num_data[$i]['student_id']." AND ".$subject_pass_statement;
        $update_pass_query=mysql_query($update_pass_statement);
    }
}

This is not about mysql_fetch_assoc function, but about the logic of your code. You are having hard time, mainly because of how your code looks. You are facing too many problems at once, the most reasonable solution here is to:

decompose the problem

  1. Think about what are the main tasks / actions that this code will do.
    Let's say:
    1. For every student, we retrieve optional subjects they enrolled.
    2. Then we retrieve their main subjects they were taking.
    3. And then we retrieve their results to determine which subjects they have passed and which they did not.
  2. Start with code that expresses your intetion, use functions that don't exist yet:

     $passStatementForYear = new PassStatement($year);
     $students = getStudentsForSchoolYear($year);
     foreach ($students as $student) {
         $optionalSubjects = loadOptionalSubjectsForStudent($student);
         $mainSubjects = loadMainSubjectsForStudent($student);
         $passReport = prepareReportForStudent(
             $student, $optionalSubjects, $mainSubjects
         );
         updatePassStatementUsingStudentsReport(
             $passStatementForYear, $passReport
         );
     }
    
  3. Build missing parts (functions, classes etc.)

Now you just need to take care of those standalone steps. Every function introduces new small problem, which you can take care of in isolate and simple manner. Only then, if you find yourself having issues with mysql_fetch_assoc, refer to the official documentation and example they show:

$sql = "SELECT id as userid, fullname, userstatus
        FROM   sometable
        WHERE  userstatus = 1";

$result = mysql_query($sql);

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}

mysql_free_result($result);