简体   繁体   English

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

[英]How to use mysql_fetch_assoc to process records in php?

在此处输入图片说明

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. 通过使用这些主题ID,我可以从主题表中检索名称。 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. 这与mysql_fetch_assoc函数无关,而与代码的逻辑有关。 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: 只有这样,如果您发现mysql_fetch_assoc问题,请参考官方文档及其显示的示例:

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM