0

我正在尝试解决如何将 $sql_pay_det 查询移到 $sql_pay 循环之外(因此实际上查询 $rs_pay_det 而不是为循环的每次迭代创建一个新的 $rs_pay_det )

目前我的代码如下所示:

        //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, ".", ",");

                    }
                }
            }

我试过这样做:

        //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 ('.$sql_pay_det.') tpd WHERE EmployeeRef = "'.$assign_no.'"';
                    $rs_pay_det2 = mysql_query($sql_pay_det2);
                    $row_pay_det2 = mysql_fetch_assoc($rs_pay_det2);

                while($row_pay_det2 = mysql_fetch_array($rs_pay_det2))
                {
                    $element_det = $row_pay_det2['ElementDesc'];
                    $amount_det = $row_pay_det2['Amount'];
                    $wte_worked = $row_pay_det2['WTEWorked'];
                    $wte_paid = $row_pay_det2['WTEPaid'];
                    $wte_cont = $row_pay_det2['WTEContract'];
                    $payscale = $row_pay_det2['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, ".", ",");

                    }
                }
            }

但是我在 $row_pay_det2 上收到一个错误,说“mysql_fetch_assoc():提供的参数不是有效的 MySQL 结果资源”

4

1 回答 1

0

您的subquery语法 ( ... FROM "'.$rs_pay_det.'" ...) 是错误的。

根据手册 - dev.mysql.com/doc/refman/5.0/en/from-clause-subqueries.html

子查询在 SELECT 语句的 FROM 子句中是合法的。实际语法是:

SELECT ... FROM (子查询) [AS] 名称 ...

尝试更改为-

$sql_pay_det2 = 'SELECT ElementDesc, Amount, WTEWorked, WTEPaid, WTEContract, Payscale FROM ('.$sql_pay_det.') tpd WHERE EmployeeRef = "'.$assign_no.'"';

编辑 - 问题 #2

您只能获得数据集中的第二行,因为moves the internal data pointer ahead在 using 之后有重复的代码(mysql_fetch_assoc() 和 mysql_fetch_array()) mysql_fetch_assoc()。因此,当您这样做时,mysql_fetch_array()您将获得除第一行之外的每一行。

    //Get payroll transactions
    ...
    $row_pay = mysql_fetch_assoc($rs_pay);

    //Get payroll detail recordset
    ...

        while($row_pay = mysql_fetch_array($rs_pay))
          {
                ...
                $row_pay_det2 = mysql_fetch_assoc($rs_pay_det2);

            while($row_pay_det2 = mysql_fetch_array($rs_pay_det2))
            {
                ...
                }
            }
        }

您需要删除 - $row_pay = mysql_fetch_assoc($rs_pay);&$row_pay_det2 = mysql_fetch_assoc($rs_pay_det2);

于 2012-12-09T18:56:51.327 回答