Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8977877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:28:29+00:00 2026-06-15T19:28:29+00:00

I’m trying to work out how to move the $sql_pay_det query outside of the

  • 0

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 ('.$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, ".", ",");

                    }
                }
            }

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:28:31+00:00Added an answer on June 15, 2026 at 7:28 pm

    Your subquery syntax (... FROM "'.$rs_pay_det.'" ...) is wrong.

    According to the manual – dev.mysql.com/doc/refman/5.0/en/from-clause-subqueries.html

    Subqueries are legal in a SELECT statement’s FROM clause. The actual
    syntax is:

    SELECT … FROM (subquery) [AS] name …

    Try changing to-

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

    Edit – Issue #2

    You are only getting the 2nd row in your data set as you have duplicate code (mysql_fetch_assoc() & mysql_fetch_array()) that moves the internal data pointer ahead after the using mysql_fetch_assoc(). So when you do mysql_fetch_array() you will get every row except the 1st row.

        //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))
                {
                    ...
                    }
                }
            }
    

    You need to remove – $row_pay = mysql_fetch_assoc($rs_pay); & $row_pay_det2 = mysql_fetch_assoc($rs_pay_det2);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.