mysql - How to use mysql_fetch_assoc to process records in php? -
i trying retrieve subject names, there 2 types of subjects optional , main. each student contains optional , main subjects. using subject ids can retrieve name subject table. in result 1 optional subject name appearing, there problem in code? pls me
$subject_names = array(); for($i=0;$i<count($student_num_data);$i++) { $optional_id_list = mysql_query("select optional_subject_id ms_student student_id = ".$student_num_data[$i]['student_id']); while($row = mysql_fetch_assoc($optional_id_list)) { foreach ($row $key) { $optional_subject = mysql_query("select subject_name ms_subject 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 subject_config stream_index =".$stu_stream); while($row = mysql_fetch_assoc($subject_id_list)) { foreach ($row $key) { $main_subject = mysql_query("select subject_name ms_subject 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 @ end of string $small_subject_name = str_replace(" ", "-", $small_subject_name); // replaces spaces 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 hyphens 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)) ".$small_subject_name.","; $sum_subject_statement = $sum_subject_statement.'ifnull(`'.$small_subject_name.'`,0) + '; /* start 1022 02-09-2014 */ $subject_pass_statement .= $small_subject_name." >= 33 , "; $subject_pass_statement = substr($subject_pass_statement,0,strlen($subject_pass_statement)-5); $update_pass_statement = "update ".$table_name." set class_rank = 'p' 1=1 ".$track_div_stmnt.$id_stmnt." , student_id = ".$student_num_data[$i]['student_id']." , ".$subject_pass_statement; $update_pass_query=mysql_query($update_pass_statement); } }
this not mysql_fetch_assoc
function, logic of code. having hard time, because of how code looks. facing many problems @ once, reasonable solution here to:
decompose problem
- think main tasks / actions code do.
let's say:- for every student, retrieve optional subjects enrolled.
- then retrieve main subjects taking.
- and retrieve results determine subjects have passed , did not.
start code expresses intetion, use functions don't exist yet:
$passstatementforyear = new passstatement($year); $students = getstudentsforschoolyear($year); foreach ($students $student) { $optionalsubjects = loadoptionalsubjectsforstudent($student); $mainsubjects = loadmainsubjectsforstudent($student); $passreport = preparereportforstudent( $student, $optionalsubjects, $mainsubjects ); updatepassstatementusingstudentsreport( $passstatementforyear, $passreport ); }
- build missing parts (functions, classes etc.)
now need take care of standalone steps. every function introduces new small problem, can take care of in isolate , simple manner. then, if find having issues mysql_fetch_assoc
, refer official documentation , example show:
$sql = "select id userid, fullname, userstatus sometable userstatus = 1"; $result = mysql_query($sql); if (!$result) { echo "could not run query ($sql) db: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "no rows found, nothing print exiting"; exit; } // while row of data exists, put row in $row associative array // note: if you're expecting 1 row, no need use loop while ($row = mysql_fetch_assoc($result)) { echo $row["userid"]; echo $row["fullname"]; echo $row["userstatus"]; } mysql_free_result($result);
Comments
Post a Comment