Query a recordset

Posted by Dion on Stack Overflow See other posts from Stack Overflow or by Dion
Published on 2012-12-09T17:01:41Z Indexed on 2012/12/09 17:03 UTC
Read the original article Hit count: 216

Filed under:
|

I'm trying to work out how to move the $sql_pay_det query outside of the $sql_pay loop (so in effect query on $rs_pay_det rather than creating a new $rs_pay_det for each iteration of the loop)

At the moment my code looks like:

        //Get payroll transactions
        $sql_pay = 'SELECT Field1, DescriptionandURLlink, Field5, Amount, Field4, PeriodName FROM tblgltransactionspayroll WHERE CostCentreCode = "'.$cc.'" AND SubjectiveCode = "'.$subj.'" AND PeriodNo = "'.$month.'"';
        $rs_pay = mysql_query($sql_pay);
        $row_pay = mysql_fetch_assoc($rs_pay);

            while($row_pay = mysql_fetch_array($rs_pay))
              {
                    $employee_name = $row_pay['Field1'];
                    $assign_no = $row_pay['DescriptionandURLlink'];
                    $pay_period = $row_pay['Field5'];
                    $mth_name = $row_pay['PeriodName'];
                    $amount = $row_pay['Amount'];
                    $total_amount = $total_amount + $amount;
                    $amount = my_number_format($amount, 2, ".", ",");

                    $sql_pay_det = 'SELECT ElementDesc, Amount, WTEWorked, WTEPaid, WTEContract, Payscale FROM tblpayrolldetail WHERE CostCentreCode = "'.$cc.'" AND SubjectiveCode = "'.$subj.'" AND AccountingPeriod = "'.$mth_name.'" AND EmployeeRef = "'.$assign_no.'"';
                    $rs_pay_det = mysql_query($sql_pay_det);
                    $row_pay_det = mysql_fetch_assoc($rs_pay_det);

                while($row_pay_det = mysql_fetch_array($rs_pay_det))
                {
                    $element_det = $row_pay_det['ElementDesc'];
                    $amount_det = $row_pay_det['Amount'];
                    $wte_worked = $row_pay_det['WTEWorked'];
                    $wte_paid = $row_pay_det['WTEPaid'];
                    $wte_cont = $row_pay_det['WTEContract'];
                    $payscale = $row_pay_det['Payscale'];

                    //Get band/point and annual salary where element is basic pay
                    if ($element_det =="3959#Basic Pay"){
                        $sql_payscale = 'SELECT txtPayscaleName, Salary FROM tblpayscalemapping WHERE txtPayscale = "'.$payscale.'"';
                        $rs_payscale = mysql_query($sql_payscale);
                        $row_payscale = mysql_fetch_assoc($rs_payscale);

                        $grade = $row_payscale['txtPayscaleName'];
                        $salary = "£" . my_number_format($row_payscale['Salary'], 0, ".", ",");

                    }
                }
            }

I've tried doing this:

        //Get payroll transactions
        $sql_pay = 'SELECT Field1, DescriptionandURLlink, Field5, Amount, Field4, PeriodName FROM tblgltransactionspayroll WHERE CostCentreCode = "'.$cc.'" AND SubjectiveCode = "'.$subj.'" AND PeriodNo = "'.$month.'"';
        $rs_pay = mysql_query($sql_pay);
        $row_pay = mysql_fetch_assoc($rs_pay);

        //Get payroll detail recordset
        $sql_pay_det = 'SELECT ElementDesc, Amount, WTEWorked, WTEPaid, WTEContract, Payscale, EmployeeRef FROM tblpayrolldetail WHERE CostCentreCode = "'.$cc.'" AND SubjectiveCode = "'.$subj.'" AND AccountingPeriod = "'.$mth_name.'"';
        $rs_pay_det = mysql_query($sql_pay_det);

            while($row_pay = mysql_fetch_array($rs_pay))
              {
                    $employee_name = $row_pay['Field1'];
                    $assign_no = $row_pay['DescriptionandURLlink'];
                    $pay_period = $row_pay['Field5'];
                    $mth_name = $row_pay['PeriodName'];
                    $amount = $row_pay['Amount'];
                    $total_amount = $total_amount + $amount;
                    $amount = my_number_format($amount, 2, ".", ",");

                    //Query $rs_pay_det
                    $sql_pay_det2 = 'SELECT ElementDesc, Amount, WTEWorked, WTEPaid, WTEContract, Payscale FROM "'.$rs_pay_det.'" WHERE EmployeeRef = "'.$assign_no.'"';
                    $rs_pay_det2 = mysql_query($sql_pay_det2);
                    $row_pay_det2 = mysql_fetch_assoc($rs_pay_det2);

                while($row_pay_det = mysql_fetch_array($rs_pay_det))
                {
                    $element_det = $row_pay_det['ElementDesc'];
                    $amount_det = $row_pay_det['Amount'];
                    $wte_worked = $row_pay_det['WTEWorked'];
                    $wte_paid = $row_pay_det['WTEPaid'];
                    $wte_cont = $row_pay_det['WTEContract'];
                    $payscale = $row_pay_det['Payscale'];

                    //Get band/point and annual salary where element is basic pay
                    if ($element_det =="3959#Basic Pay"){
                        $sql_payscale = 'SELECT txtPayscaleName, Salary FROM tblpayscalemapping WHERE txtPayscale = "'.$payscale.'"';
                        $rs_payscale = mysql_query($sql_payscale);
                        $row_payscale = mysql_fetch_assoc($rs_payscale);

                        $grade = $row_payscale['txtPayscaleName'];
                        $salary = "£" . my_number_format($row_payscale['Salary'], 0, ".", ",");

                    }
                }
            }

But I get an error on $row_pay_det2 saying that "mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource"

© Stack Overflow or respective owner

Related posts about php

Related posts about sql